Android example start activity componentinfo

The app crashes because it cannot create the viewmodel instance #145

Comments

WasimReza2K8 commented Dec 5, 2020

After running the code I got the following error message from android studio. Basically, it cannot create the viewmodel because it has a dependency on the application reference. However, the initialization did not mention from where the viewmodel collects the dependency.

java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.RuntimeException: Cannot create an instance of class com.example.android.roomwordssample.WordViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
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:49)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.InstantiationException: java.lang.Class has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)

The text was updated successfully, but these errors were encountered:

florina-muntenescu commented Dec 6, 2020

Which version of the codelab is this, Java or Kotlin?

Читайте также:  Как подключить устройство android studio

Akiva-Grobman commented Dec 7, 2020 •

I had the same problem(in java). This solved it:
// old code
mWordViewModel = new ViewModelProvider(this).get(WordViewModel.class);
// new code
mWordViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(WordViewModel.class);

Akiva-Grobman commented Dec 7, 2020

Check out my previous comment and see if it help.

hansRapnhap commented Dec 20, 2020 •

Akiva’s solution helps. Why do you not include that in the github code and in the
https://developer.android.com/codelabs/android-room-with-a-view
pages?
I followed the complete course, and ended in loosing time by this not working code part !

Akiva-Grobman commented Dec 20, 2020

Give them some time, I’m sure it will be fixed.

flypadre commented Jan 23, 2021

For anyone who doesn’t know the line of code can be found in MainActivity.java

KJH1964 commented Jan 25, 2021 •

Thanks, I’d figured out it was in that line of code, just didn’t know what to do about it. I’d tracked down the exception being thrown in the ActivityThread.java class when that line was executed. But it would have taken me a long time to get from there to the change above. As hansRapnhap says it would be nice if they updated GitHub. Still I learnt some more about tracking down bugs, even if I didn’t fix it.

Akiva-Grobman commented Jan 25, 2021

Thanks, I’d figured out it was in that line of code, just didn’t know what to do about it. I’d tracked down the exception being thrown in the ActivityThread.java class when that line was executed. But it would have taken me a long time to get from there to the change above. As hansRapnhap says it would be nice if they updated GitHub. Still I learnt some more about tracking down bugs, even if I didn’t fix it.

I’ve made a PR but it doesn’t pass a firebase test so they didn’t except it.

Читайте также:  Как вернуть все настройки андроида по умолчанию

SharifMrCreed commented Feb 22, 2021

I had the same problem(in java). This solved it:
// old code
mWordViewModel = new ViewModelProvider(this).get(WordViewModel.class);
// new code
mWordViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(WordViewModel.class);

Man thanx alot for this been at it for almost 2 days to thing it would be solved so simply like this.

Источник

Оцените статью