gpt4 book ai didi

java - 无法创建 AndroidViewModel 子类的实例

转载 作者:太空宇宙 更新时间:2023-11-04 10:10:33 25 4
gpt4 key购买 nike

我的 ViewModel 类是用 Java 编写的,我的 Activity 类是用 Kotlin 编写的。我收到错误

09-18 14:01:32.737 14082-14082/com.meme.archcompwordlist E/AndroidRuntime: FATAL EXCEPTION: main Process: com.meme.archcompwordlist, PID: 14082 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.meme.archcompwordlist/com.meme.archcompwordlist.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.meme.archcompwordlist.WordViewModel at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.RuntimeException: Cannot create an instance of class com.meme.archcompwordlist.WordViewModel at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:207) at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134) at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102) at com.meme.archcompwordlist.MainActivity.onCreate(MainActivity.kt:37) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:430) at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:199) at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134)  at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102)  at com.meme.archcompwordlist.MainActivity.onCreate(MainActivity.kt:37)  at android.app.Activity.performCreate(Activity.java:6679)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  Caused by: java.lang.RuntimeException: cannot find implementation for com.meme.archcompwordlist.room.WordRoomDatabase. WordRoomDatabase_Impl does not exist at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:93) at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:630) at com.meme.archcompwordlist.room.WordRoomDatabase.getRoomDatabase(WordRoomDatabase.java:22) at com.meme.archcompwordlist.WordRepository.(WordRepository.java:21) at com.meme.archcompwordlist.WordViewModel.(WordViewModel.java:22) at java.lang.reflect.Constructor.newInstance0(Native Method)  at java.lang.reflect.Constructor.newInstance(Constructor.java:430)  at android.arch.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:199)  at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134)  at android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102)  at com.meme.archcompwordlist.MainActivity.onCreate(MainActivity.kt:37)  at android.app.Activity.performCreate(Activity.java:6679)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

这是我的 AndroidViewModel 类

public class WordViewModel extends AndroidViewModel {

private WordRepository repository;

private LiveData<List<WordEntity>> allWords;

public WordViewModel(Application application){
super(application);
repository = new WordRepository(application);
allWords = repository.getAllWords();
}

public LiveData<List<WordEntity>> getAllWords(){
return allWords;
}

public void insert(WordEntity word){
repository.insert(word);
}

public void deleteAllWords() {
repository.deleteAllWords();
}
}

这是我的 Activity 课

class MainActivity : AppCompatActivity() {

private lateinit var wordViewModel:WordViewModel
private lateinit var newWord:EditText
private lateinit var recyclerView:RecyclerView
private lateinit var adapter:WordAdapter


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)

adapter= WordAdapter()
recyclerView= findViewById<RecyclerView>(R.id.wordList)
recyclerView.adapter=adapter

//establish a connection to the current ViewModel
wordViewModel= ViewModelProviders.of(this).get(WordViewModel::class.java)

newWord = findViewById<EditText>(R.id.insertWord);
findViewById<Button>(R.id.submitWord).setOnClickListener {
fun onClick(v:View?){
if(!TextUtils.isEmpty(newWord.getText())){
wordViewModel.insert(WordEntity(newWord.getText().toString()))
}
}
}
findViewById<Button>(R.id.deleteWords).setOnClickListener{
fun onClick(v:View?){
wordViewModel.deleteAllWords()
}
}


wordViewModel.getAllWords().observe(this,Observer<List<WordEntity>>(){
fun onChanged(words:List<WordEntity>){
adapter.setWords(words)
}
})

}

}

有人可以帮我找出我做错了什么吗?离开 Android 很长一段时间后,我正在尝试学习架构组件。我不完全清楚如何继续。

最佳答案

我遇到了同样的问题并从中找到了解决方案blog .

您应该将 ViewModelProvider.AndroidViewModelFactory 传递给 ViewModelProvider,如下所示:

WordViewModel model = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(WordViewModel.class);


关于java - 无法创建 AndroidViewModel 子类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52394416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com