无法创建AndroidViewModel的子类的实例

时间:2018-09-18 20:49:07

标签: java android kotlin android-viewmodel

我的ViewModel类在Java中,而我的Activity类在Kotlin中。我收到错误

  

09-18 14:01:32.737 14082-1​​4082 / com.meme.archcompwordlist   E / AndroidRuntime:致命异常:主要过程:   com.meme.archcompwordlist,PID:14082 java.lang.RuntimeException:   无法开始活动   ComponentInfo {com.meme.archcompwordlist / com.meme.archcompwordlist.MainActivity}:   java.lang.RuntimeException:无法创建类的实例   com.meme.archcompwordlist.WordViewModel       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)       在android.app.ActivityThread.-wrap12(ActivityThread.java)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)       在android.os.Handler.dispatchMessage(Handler.java:102)       在android.os.Looper.loop(Looper.java:154)       在android.app.ActivityThread.main(ActivityThread.java:6119)       在java.lang.reflect.Method.invoke(本机方法)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)       com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)的原因:java.lang.RuntimeException:无法创建的实例   com.meme.archcompwordlist.WordViewModel类       在android.arch.lifecycle.ViewModelProvider $ AndroidViewModelFactory.create(ViewModelProvider.java:207)       在android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134)       在android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102)       在com.meme.archcompwordlist.MainActivity.onCreate(MainActivity.kt:37)       在android.app.Activity.performCreate(Activity.java:6679)       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)       在android.app.ActivityThread.-wrap12(ActivityThread.java)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)       在android.os.Handler.dispatchMessage(Handler.java:102)       在android.os.Looper.loop(Looper.java:154)       在android.app.ActivityThread.main(ActivityThread.java:6119)       在java.lang.reflect.Method.invoke(本机方法)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)上由:java.lang.reflect.InvocationTargetException       在java.lang.reflect.Constructor.newInstance0(本地方法)       在java.lang.reflect.Constructor.newInstance(Constructor.java:430)       在android.arch.lifecycle.ViewModelProvider $ AndroidViewModelFactory.create(ViewModelProvider.java:199)       在android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134)       在android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102)       在com.meme.archcompwordlist.MainActivity.onCreate(MainActivity.kt:37)       在android.app.Activity.performCreate(Activity.java:6679)       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)       在android.app.ActivityThread.-wrap12(ActivityThread.java)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)       在android.os.Handler.dispatchMessage(Handler.java:102)       在android.os.Looper.loop(Looper.java:154)       在android.app.ActivityThread.main(ActivityThread.java:6119)       在java.lang.reflect.Method.invoke(本机方法)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)导致:java.lang.RuntimeException:找不到实现   com.meme.archcompwordlist.room.WordRoomDatabase。 WordRoomDatabase_Impl   不存在       在android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:93)       在android.arch.persistence.room.RoomDatabase $ Builder.build(RoomDatabase.java:630)       在com.meme.archcompwordlist.room.WordRoomDatabase.getRoomDatabase(WordRoomDatabase.java:22)       在com.meme.archcompwordlist.WordRepository。(WordRepository.java:21)       在com.meme.archcompwordlist.WordViewModel。(WordViewModel.java:22)       在java.lang.reflect.Constructor.newInstance0(本地方法)       在java.lang.reflect.Constructor.newInstance(Constructor.java:430)       在android.arch.lifecycle.ViewModelProvider $ AndroidViewModelFactory.create(ViewModelProvider.java:199)       在android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:134)       在android.arch.lifecycle.ViewModelProvider.get(ViewModelProvider.java:102)       在com.meme.archcompwordlist.MainActivity.onCreate(MainActivity.kt:37)       在android.app.Activity.performCreate(Activity.java:6679)       在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)       在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)       在android.app.ActivityThread.-wrap12(ActivityThread.java)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)       在android.os.Handler.dispatchMessage(Handler.java:102)       在android.os.Looper.loop(Looper.java:154)       在android.app.ActivityThread.main(ActivityThread.java:6119)       在java.lang.reflect.Method.invoke(本机方法)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)       在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();
    }
}

这是我的活动课

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的情况下,我想尝试使用Architecture Components。我还不清楚如何进行。

1 个答案:

答案 0 :(得分:4)

我遇到了同样的问题,并从此blog找到了解决方法。

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

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