Android Room with a View - Java triggers error: WordViewModel has no zero argument constructor

Android Room with a View - Java triggers error: WordViewModel has no zero argument constructor

我已经完成了 github 的 Android Room with a View - Java codelab and wound up with an error that causes the app to crash before it loads (see "fatal exception", below). Thinking perhaps I did something wrong, I copied the contents of each of the .java solution files

我尝试使用另一个不带参数的构造函数创建重载 WordViewModel class,但这只会产生更多问题。

public WordViewModel( ) {
    super(new Application());
    mRepository = new WordRepository(new Application());
    mAllWords = mRepository.getAllWords();
}

致命异常:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.roomwordssample, PID: 32145
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.roomwordssample/com.example.android.roomwordssample.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.android.roomwordssample.WordViewModel
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.android.roomwordssample.WordViewModel
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
    at com.example.android.roomwordssample.MainActivity.onCreate(MainActivity.java:32)
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
 Caused by: java.lang.InstantiationException: java.lang.Class<com.example.android.roomwordssample.WordViewModel> has no zero argument constructor
    at java.lang.Class.newInstance(Native Method)
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187) 
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) 
    at com.example.android.roomwordssample.MainActivity.onCreate(MainActivity.java:32) 
    at android.app.Activity.performCreate(Activity.java:8000) 
    at android.app.Activity.performCreate(Activity.java:7984) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

知道是什么触发了这个吗?

更新: 还尝试从 github 上的解决方案复制应用程序和项目 build.gradle 文件。为了让它编译,我必须将 minSdkVersion 更改为 21(从 20)以解决有关矢量图形的构建错误。还将依赖项更改为 gradle 版本 4.1.0(从解决方案中的 3.4.3)。编译,但 运行 应用程序触发与上述相同的错误。

更新#2 还尝试复制 gradle-wrapper.properties(引用 gradle-5.1.1-all 发行版)并将 buid.gradle 恢复到解决方案的版本(引用 Gradle 插件版本 3.4.3)。仍然没有骰子。

更新#3 我下载了 entire solution project from GitHub 并将其导入到 Android Studio 中的新项目中。它编译但生成完全相同的 no zero argument constructor 错误。

GitHub issues tab for this project 上找到了一个修复程序,它为我解决了这个问题: 只需打开 MainActivity.java 并在 onCreate 中找到:

mWordViewModel = new ViewModelProvider(this).get(WordViewModel.class)

并将其更改为:

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

默认情况下 viewModel 接受零参数,这意味着 viewModel 中没有参数只是一个空的构造函数
但是如果你想在 viewModel 构造函数中传递一些东西。
有两种方式:
1-> AndroidViewModel: 它只接受一种类型的参数,它是 applicationContext

class MainViewModel(application: Application): AndroidViewModel(application) {}

请记住:当您使用 androidViewModel

时,构造函数参数中不能传递其他值 2-> ViewModelFactory:您将使用常规 viewModel 但您正在制作额外的 viewModelFactory class:
这是 viewModelFactory:
的完整实现 ViewModel

class MainViewModel(val value: Int): ViewModel() {}

ViewModelFactory:

  class ViewModelFactory(
       private val value: Int     
        ): ViewModelProvider.Factory {
        override fun <T : ViewModel?> create(modelClass: Class<T>): T {
            if (modelClass.isAssignableFrom(MainViewModel::class.java)){
                return MainViewModel(value) as T
            }
            throw IllegalArgumentException("Invalid model class")
        }
    }

你的Activity:

lateinit var viewModel : MainViewModel
private val viewModelFactory: ViewModelProvider.Factory = ViewModelFactory(10)

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 viewModel = ViewModelProvider(this, viewModelFactory)[MainViewModel::class.java]
}