无法创建类的实例-ViewModel

时间:2018-06-21 12:50:35

标签: android kotlin viewmodel android-architecture-components

我正在跟踪google的示例,该示例在Java中,但是当我在kotlin中重写它时,无法实例化该类。 我正在学习视图模型。我是否怀疑我的 kotlin 代码的语法是否正确。

https://github.com/googlecodelabs/android-room-with-a-view

Error       类MainActivity:AppCompatActivity(){

//    private lateinit var mWordViewModel: WordViewModel //not works
    private var mWordViewModel: WordViewModel? = null //not works
    private var wordListAdapter: WordListAdapter?= null

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

        try {
            mWordViewModel = ViewModelProviders.of(this@MainActivity).get(WordViewModel::class.java)
        }catch (e: Exception){
            e.printStackTrace()
        }

...

class WordViewModel(application: Application): AndroidViewModel(application) {

//    super(application)

    private var mRepository: WordRepository? = null
    private var mAllWords: LiveData<List<Word>>? = null

    init {
        mRepository = WordRepository(application)
        mAllWords = mRepository?.getAllWords()
    }

...

class WordRepository(application: Application) {

    private var mWordDao: WordDao? = null
    private var mAllWords: LiveData<List<Word>>? = null

    init {
        val db = WordRoomDatabase.getDatabase(application)
        mWordDao = db.wordDao()
        mAllWords = mWordDao!!.getAlphabetizeWords()
    }

...

@Database(entities = arrayOf(Word::class), version = 1)
abstract class WordRoomDatabase: RoomDatabase(){

    abstract fun wordDao(): WordDao

    companion object {
        @JvmStatic
        private var INSTANCE: WordRoomDatabase? = null

        fun getDatabase(context: Context): WordRoomDatabase {
            if (INSTANCE == null) {
                synchronized(WordRoomDatabase::class.java) {
                    if (INSTANCE == null) {
                        INSTANCE = Room.databaseBuilder(context.applicationContext, WordRoomDatabase::class.java, "word_database")
                                .fallbackToDestructiveMigration()
                                .addCallback(sRoomDatabaseCallback)
                                .build()
                    }
                }
            }
            return INSTANCE!!
        }

2 个答案:

答案 0 :(得分:2)

尽管如此,我在JAVA中也遇到了同样的问题。我的问题通过使用ViewModelFactory如下解决:

public class MyViewModelFactory implements ViewModelProvider.Factory {
    private Application mApplication;



    public MyViewModelFactory(Application application) {
        mApplication = application;

    }
    @Override
    public <T extends ViewModel> T create(Class<T> modelClass) {
        return (T) new BookViewModel(mApplication);
    }

在调用ViewModel的地方,将其更改为:

mBookViewModel = new ViewModelProvider(this, new MyViewModelFactory
                (this.getApplication())).get(BookViewModel.class);

您可以从本教程的Github问题页面上查看此链接,以查找该问题: https://github.com/googlecodelabs/android-room-with-a-view/issues

答案 1 :(得分:1)

@zunjae

错误

1-语法代码koltin。

2-项目配置。

class MainActivity: AppCompatActivity() {
mWordViewModel = ViewModelProviders.of(this@MainActivity).get(WordViewModel(application)::class.java)

...

class WordViewModel constructor (application: Application):  AndroidViewModel(application){

...

class WordRepository constructor(application: Application) {

...

添加应用程序build.gradle

apply plugin: 'kotlin-kapt'

kapt "android.arch.lifecycle:extensions:$rootProject.archLifecycleVersion"
kapt "android.arch.persistence.room:compiler:$rootProject.roomVersion"