AkshayChordiya / News

A sample News 🗞 app built using Modern Android Development [Architecture Components, Coroutines, Retrofit, Room, Kotlin, Dagger]
MIT License
848 stars 168 forks source link

Caused by: java.lang.RuntimeException: Cannot create an instance of class com.akshay.newsapp.news.ui.viewmodel.NewsArticleViewModel #33

Closed akash09766 closed 4 years ago

akash09766 commented 4 years ago

gettting below error at first launch after import in to android studio

2020-02-27 15:16:26.804 23096-23096/com.akshay.newsapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.akshay.newsapp, PID: 23096 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.akshay.newsapp/com.akshay.newsapp.news.ui.activity.NewsActivity}: java.lang.RuntimeException: Cannot create an instance of class com.akshay.newsapp.news.ui.viewmodel.NewsArticleViewModel at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2952) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3087) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1817) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6746) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.RuntimeException: Cannot create an instance of class com.akshay.newsapp.news.ui.viewmodel.NewsArticleViewModel at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221) at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278) at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:106) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185) at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) at com.akshay.newsapp.news.ui.activity.NewsActivity$newsArticleViewModel$2.invoke(NewsActivity.kt:50) at com.akshay.newsapp.news.ui.activity.NewsActivity$newsArticleViewModel$2.invoke(NewsActivity.kt:18) at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) at com.akshay.newsapp.news.ui.activity.NewsActivity.getNewsArticleViewModel(Unknown Source:7) at com.akshay.newsapp.news.ui.activity.NewsActivity.onCreate(NewsActivity.kt:39) at android.app.Activity.performCreate(Activity.java:7144) at android.app.Activity.performCreate(Activity.java:7135) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2932) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3087)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1817)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:6746)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)  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) at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)  at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:106)  at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)  at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)  at com.akshay.newsapp.news.ui.activity.NewsActivity$newsArticleViewModel$2.invoke(NewsActivity.kt:50)  at com.akshay.newsapp.news.ui.activity.NewsActivity$newsArticleViewModel$2.invoke(NewsActivity.kt:18)  at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)  at com.akshay.newsapp.news.ui.activity.NewsActivity.getNewsArticleViewModel(Unknown Source:7)  at com.akshay.newsapp.news.ui.activity.NewsActivity.onCreate(NewsActivity.kt:39)  at android.app.Activity.performCreate(Activity.java:7144)  at android.app.Activity.performCreate(Activity.java:7135)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2932)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3087)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1817)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:6746)  at java.lang.reflect.Method.invoke(Native Method)

AkshayChordiya commented 4 years ago

Can you please try the latest master and let me know if that works for you? Thanks

akash09766 commented 4 years ago

Now Its build working. if i want to add support for fragment then, is it right approach ?

inline fun Fragment.getViewModel(): T { return ViewModelProvider(this, (activity as BaseActivity).viewModelFactory).get(T::class.java) }

akash09766 commented 4 years ago

found it. Actually i was implementing HasAndroidInjector instead of Injectable in BaseFragment.

AkshayChordiya commented 4 years ago

Yeah you need to implement the Injectable interface. Maybe we can only rely on HasAndroidInjector, I'll take a look to update if possible.

Just so you know there is a difference between getting view model:

// View model is scoped to parent activity
inline fun Fragment.getViewModel(): T {
   return ViewModelProvider(this.activity, (activity as BaseActivity).viewModelFactory).get(T::class.java)
}

// View model is scoped to fragment
inline fun Fragment.getViewModel(): T {
   return ViewModelProvider(this, (activity as BaseActivity).viewModelFactory).get(T::class.java)
}