无法创建 ViewModel 类的实例
Posted
技术标签:
【中文标题】无法创建 ViewModel 类的实例【英文标题】:Can not create instance of the ViewModel class 【发布时间】:2021-11-28 02:20:09 【问题描述】:您好,我正在使用房间数据库创建相册应用程序。但是当我运行应用程序时,我得到一个错误,说不能创建 viewmodel 类的实例。您的帮助将不胜感激。 这是日志猫
java.lang.RuntimeException:无法启动活动 ComponentInfocom.example.photoalbum/com.example.photoalbum.MainActivity:java.lang.RuntimeException:无法创建 com.example.photoalbum.MyImagesVIewModel 类的实例 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:223) 在 android.app.ActivityThread.main(ActivityThread.java:7656) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 原因:java.lang.RuntimeException:无法创建 com.example.photoalbum.MyImagesVIewModel 类的实例 在 androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:236) 在 androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:171) 在 androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:139) 在 com.example.photoalbum.MainActivity.onCreate(MainActivity.java:35) 在 android.app.Activity.performCreate(Activity.java:8000) 在 android.app.Activity.performCreate(Activity.java:7984) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:223) 在 android.app.ActivityThread.main(ActivityThread.java:7656) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 引起:java.lang.reflect.InvocationTargetException 在 java.lang.reflect.Constructor.newInstance0(本机方法) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:343) 在 androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:228) 在 androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:171) 在 androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:139) 在 com.example.photoalbum.MainActivity.onCreate(MainActivity.java:35) 在 android.app.Activity.performCreate(Activity.java:8000) 在 android.app.Activity.performCreate(Activity.java:7984) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:223) 在 android.app.ActivityThread.main(ActivityThread.java:7656) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 引起:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'com.example.photoalbum.MyImagesDAO com.example.photoalbum.MyImagesDatabase.myImagesDAO()' 在 com.example.photoalbum.MyImagesRepository.(MyImagesRepository.java:19) 在 com.example.photoalbum.MyImagesVIewModel.(MyImagesVIewModel.java:22) 在 java.lang.reflect.Constructor.newInstance0(本机方法) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:343) 在 androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:228) 在 androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:171) 在 androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:139) 在 com.example.photoalbum.MainActivity.onCreate(MainActivity.java:35) 在 android.app.Activity.performCreate(Activity.java:8000) 在 android.app.Activity.performCreate(Activity.java:7984) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 在 android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java:223) 在 android.app.ActivityThread.main(ActivityThread.java:7656) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 2021-10-08 09:18:55.708 23415-23415/com.example.photoalbum I/Process:发送信号。 PID:23415 SIG:9
这里是 Viewmodel 类
package com.example.photoalbum;
import android.app.Application;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProvider;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import java.util.List;
public class MyImagesVIewModel extends AndroidViewModel
private MyImagesRepository repository;
private LiveData<List<MyImages>> imageList;
public MyImagesVIewModel(@NonNull Application application)
super(application);
repository = new MyImagesRepository(application);
imageList = repository.getAllImages();
public void insert(MyImages myImages)
repository.insert(myImages);
public void delete(MyImages myImages)
repository.delete(myImages);
public void update(MyImages myImages)
repository.update(myImages);
public LiveData<List<MyImages>> getAllImages()
return imageList;
here is my repository classs
package com.example.photoalbum;
import android.app.Application;
import android.os.AsyncTask;
import androidx.lifecycle.LiveData;
import java.util.ArrayList;
import java.util.List;
public class MyImagesRepository
private MyImagesDAO myImagesDAO;
private LiveData<List<MyImages>> imagesList;
public MyImagesRepository(Application application)
MyImagesDatabase database = MyImagesDatabase.getInstance(application);
myImagesDAO = database.myImagesDAO();
imagesList = myImagesDAO.getAllImages();
public void insert(MyImages myImages)
new InsertImageAsyncTask(myImagesDAO).execute(myImages);
public void delete(MyImages myImages)
new DeleteImageAsyncTask(myImagesDAO).execute(myImages);
public void update(MyImages myImages)
new UpdateImageAsyncTask(myImagesDAO).execute(myImages);
public LiveData<List<MyImages>> getAllImages()
return imagesList;
public static class InsertImageAsyncTask extends AsyncTask<MyImages,Void,Void>
MyImagesDAO myImagesDAO;
public InsertImageAsyncTask(MyImagesDAO myImagesDAO)
this.myImagesDAO = myImagesDAO;
@Override
protected Void doInBackground(MyImages... myImages)
myImagesDAO.insert(myImages[0]);
return null;
public static class DeleteImageAsyncTask extends AsyncTask<MyImages,Void,Void>
MyImagesDAO myImagesDAO;
public DeleteImageAsyncTask(MyImagesDAO myImagesDAO)
this.myImagesDAO = myImagesDAO;
@Override
protected Void doInBackground(MyImages... myImages)
myImagesDAO.delete(myImages[0]);
return null;
public static class UpdateImageAsyncTask extends AsyncTask<MyImages,Void,Void>
MyImagesDAO myImagesDAO;
public UpdateImageAsyncTask(MyImagesDAO myImagesDAO)
this.myImagesDAO = myImagesDAO;
@Override
protected Void doInBackground(MyImages... myImages)
myImagesDAO.update(myImages[0]);
return null;
这是我的主要活动
package com.example.photoalbum;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.List;
public class MainActivity extends AppCompatActivity
private MyImagesVIewModel myImagesVIewModel;
private RecyclerView rv;
private FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = findViewById(R.id.rv);
fab = findViewById(R.id.fab);
rv.setLayoutManager(new LinearLayoutManager(this));
MyImagesAdapter adapter = new MyImagesAdapter();
rv.setAdapter(adapter);
myImagesVIewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication())
.create(MyImagesVIewModel.class);
//ViewModelProvider.Factory
// which may create AndroidViewModel and ViewModel, which have an empty constructor.
myImagesVIewModel.getAllImages().observe(MainActivity.this, new Observer<List<MyImages>>()
@Override
public void onChanged(List<MyImages> myImages) //called when data is changed
adapter.setImagesList(myImages);
//observer updates the UI
);
fab.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent = new Intent(MainActivity.this, AddImageActivity.class);
startActivityForResult(intent, 3);
);
这是我的存储库类
package com.example.photoalbum;
import android.app.Application;
import android.os.AsyncTask;
import androidx.lifecycle.LiveData;
import java.util.ArrayList;
import java.util.List;
public class MyImagesRepository
private MyImagesDAO myImagesDAO;
private LiveData<List<MyImages>> imagesList;
public MyImagesRepository(Application application)
MyImagesDatabase database = MyImagesDatabase.getInstance(application);
myImagesDAO = database.myImagesDAO();
imagesList = myImagesDAO.getAllImages();
public void insert(MyImages myImages)
new InsertImageAsyncTask(myImagesDAO).execute(myImages);
public void delete(MyImages myImages)
new DeleteImageAsyncTask(myImagesDAO).execute(myImages);
public void update(MyImages myImages)
new UpdateImageAsyncTask(myImagesDAO).execute(myImages);
public LiveData<List<MyImages>> getAllImages()
return imagesList;
public static class InsertImageAsyncTask extends AsyncTask<MyImages,Void,Void>
MyImagesDAO myImagesDAO;
public InsertImageAsyncTask(MyImagesDAO myImagesDAO)
this.myImagesDAO = myImagesDAO;
@Override
protected Void doInBackground(MyImages... myImages)
myImagesDAO.insert(myImages[0]);
return null;
public static class DeleteImageAsyncTask extends AsyncTask<MyImages,Void,Void>
MyImagesDAO myImagesDAO;
public DeleteImageAsyncTask(MyImagesDAO myImagesDAO)
this.myImagesDAO = myImagesDAO;
@Override
protected Void doInBackground(MyImages... myImages)
myImagesDAO.delete(myImages[0]);
return null;
public static class UpdateImageAsyncTask extends AsyncTask<MyImages,Void,Void>
MyImagesDAO myImagesDAO;
public UpdateImageAsyncTask(MyImagesDAO myImagesDAO)
this.myImagesDAO = myImagesDAO;
@Override
protected Void doInBackground(MyImages... myImages)
myImagesDAO.update(myImages[0]);
return null;
【问题讨论】:
【参考方案1】:这是因为ViewModel
构造函数中的NullPointerExcepion
。
您可以在日志中看到。
java.lang.NullPointerException: Attempt to invoke virtual method 'com.example.photoalbum.MyImagesDAO com.example.photoalbum.MyImagesDatabase.myImagesDAO()' on a null object reference at com.example.photoalbum.MyImagesRepository.
在您的存储库类中
public MyImagesRepository(Application application)
MyImagesDatabase database = MyImagesDatabase.getInstance(application);
myImagesDAO = database.myImagesDAO(); //here database seems to be null.
imagesList = myImagesDAO.getAllImages();
【讨论】:
如何解决?【参考方案2】:也许你的视图模型的构造函数有问题。试试这个构造函数-
myImagesVIewModel=new ViewModelProvider(this,ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get( MyImagesVIewModel.class);
【讨论】:
以上是关于无法创建 ViewModel 类的实例的主要内容,如果未能解决你的问题,请参考以下文章
Dagger/MissingBinding java.util.Map<java.lang.Class<? extends ViewModel>,Provider<ViewMo