提问者:小点点

无法创建类的实例-ViewModel


我正在学习google的一个例子,这个例子在java但是当我用kotlin重写它时,我不能实例化这个类。我正在学习viewmodel。我很怀疑我是否搞砸了kotlin代码的语法。

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

//    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个答案

匿名用户

@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"

匿名用户

不过,我在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