Android : WindowInsetsAnimationCallback error during exec - android

I'm trying to know when the animation close of the softkeyboard is done. So i try to use this function :
val rootLayout = findViewById<ConstraintLayout>(R.id.root_layout)
rootLayout.setWindowInsetsAnimationCallback(object : WindowInsetsAnimation.Callback(DISPATCH_MODE_STOP) {
override fun onProgress(insets: WindowInsets, runningAnimations: MutableList<WindowInsetsAnimation>): WindowInsets {
//TODO("Not yet implemented")
return insets
}
override fun onEnd(animation: WindowInsetsAnimation) {
super.onEnd(animation)
val showingKeyboard = rootLayout.rootWindowInsets.isVisible(WindowInsets.Type.ime())
if (!showingKeyboard) {
println("Keyboard is now closed")
}
}
})
but when i launch the app on my phone (and several emu) i have to face to this error :
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/view/WindowInsetsAnimation$Callback;
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1251)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3222)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3459)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2046)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:225)
at android.app.ActivityThread.main(ActivityThread.java:7564)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.WindowInsetsAnimation$Callback" on path: DexPathList[[zip file "/data/app/com.example.buginsetsanimationlistener-wRvK7cMTAxdBF1RCG19tlA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.buginsetsanimationlistener-wRvK7cMTAxdBF1RCG19tlA==/lib/arm64, /system/lib64, /system/product/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:230)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at java.lang.Class.newInstance(Native Method) 
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95) 
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45) 
at android.app.Instrumentation.newActivity(Instrumentation.java:1251) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3222) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3459) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
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:2046) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:225) 
at android.app.ActivityThread.main(ActivityThread.java:7564) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
I don't understand what i should do, thanks for your answers !

I finally found my mystake. this method is for API 30+. But i've found another way to achieve my goal :
This is work when i close by myself the soft keyboard.
imm.hideSoftInputFromWindow(rootLayout.windowToken, 0, object : ResultReceiver(null) {
override fun onReceiveResult(resultCode: Int, resultData: Bundle?) {
if (resultCode == InputMethodManager.RESULT_UNCHANGED_HIDDEN ||
resultCode == InputMethodManager.RESULT_HIDDEN) {
//Soft has finished the animation
}
}
}
})
Hope this answer help someone :)

Related

Cannot create an instance of class view model - Kotlin

I'm currently working on a project to practice dependency injection with Dagger-Hilt, however I can't get my ViewModel to instantiate, I've already looked at some other questions on the same topic, most point the issue to Gradle but that didn't fix it for me, all errors in the Stacktrace are from internal classes except for the 2 lines that involve my ViewModel within the MainActivity
Here is the Stacktrace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.practiceproject, PID: 9873
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.practiceproject/com.example.practiceproject.ui.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.example.practiceproject.ui.viewmodel.DogViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.example.practiceproject.ui.viewmodel.DogViewModel
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:112)
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:146)
at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:111)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
at com.example.practiceproject.ui.MainActivity.getDogViewModel(MainActivity.kt:15)
at com.example.practiceproject.ui.MainActivity.onCreate(MainActivity.kt:22)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
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:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
Caused by: java.lang.InstantiationException: java.lang.Class<com.example.practiceproject.ui.viewmodel.DogViewModel> 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:112) 
at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:146) 
at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:111) 
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187) 
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) 
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54) 
at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41) 
at com.example.practiceproject.ui.MainActivity.getDogViewModel(MainActivity.kt:15) 
at com.example.practiceproject.ui.MainActivity.onCreate(MainActivity.kt:22) 
at android.app.Activity.performCreate(Activity.java:7802) 
at android.app.Activity.performCreate(Activity.java:7791) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
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:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
Here is the ViewModel:
class DogViewModel #Inject constructor(
private val getDefaultImages: GetDefaultImages
) : ViewModel() {
val dogModel = MutableLiveData<DogModel>()
val isLoading = MutableLiveData<Boolean>()
fun onCreate() {
viewModelScope.launch {
isLoading.value = true
val result = getDefaultImages()
if(!result.isNullOrEmpty()) {
dogModel.value = result[0]
isLoading.value = false
}
}
}
And this is where I'm declaring it:
#AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private val dogViewModel: DogViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
dogViewModel.onCreate()
dogViewModel.dogModel.observe(this) {
initRecyclerView(it)
}
}
You should add #HiltViewModel annotation on top of your view model.
Make sure you have written #AndroidEntryPoint above your Fragment or Activity class.
You have to write #HiltViewModel above your ViewModel Class.
Also make sure you are providing all the dependencies written in ViewModel like repositories, etc.
If don't use getDefaultImages object in DogViewModel you can remove it and it will work. If you use it you need to pass an instance of GetDefaultImages class to the DogViewModel using ViewModelProvider.Factory:
#Suppress("UNCHECKED_CAST")
private val factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return DogViewModel(GetDefaultImages()) as T
}
}
private val dogViewModel: MyViewModel by viewModels { factory }

Unable to create androidviewmodel instance [duplicate]

This question already has answers here:
Android room persistent: AppDatabase_Impl does not exist
(29 answers)
Closed 12 months ago.
I am working on a notes application with room and ViewModel but while initializing the ViewModel inside activity, I am getting this
java.lang.RuntimeException: Cannot create an instance of class package.notes.homeScreen.HomeScreenVM
here is my main activity
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var viewModel: HomeScreenVM
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setUI()
setViewModel()
}
private fun setUI() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
private fun setViewModel() {
viewModel = ViewModelProvider(this, HomeScreenViewModelFactory(this.application))[HomeScreenVM::class.java]
}
}
and ViewModel is like-
class HomeScreenVM(application: Application) : AndroidViewModel(application) {
//some methods
}
and my viewmodelfactory is like-
class HomeScreenViewModelFactory(private val application: android.app.Application) : ViewModelProvider.AndroidViewModelFactory(application) {
#Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(HomeScreenVM::class.java))
return HomeScreenVM(application) as T
throw IllegalArgumentException("Unknown model class")
}
}
I don't know where the problem is. Help me.
Thanks in advance.
Update:-
full stacktrace is
Process: com.nitkkr.sanjay.notes, PID: 32750
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nitkkr.sanjay.notes/com.nitkkr.sanjay.notes.homeScreen.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.nitkkr.sanjay.notes.homeScreen.HomeScreenVM
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3536)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3708)
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:2143)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:8087)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.nitkkr.sanjay.notes.homeScreen.HomeScreenVM
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:315)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:273)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:182)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:151)
at com.nitkkr.sanjay.notes.homeScreen.MainActivity.setViewModel(MainActivity.kt:29)
at com.nitkkr.sanjay.notes.homeScreen.MainActivity.onCreate(MainActivity.kt:17)
at android.app.Activity.performCreate(Activity.java:8157)
at android.app.Activity.performCreate(Activity.java:8129)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3509)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3708) 
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:2143) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:236) 
at android.app.ActivityThread.main(ActivityThread.java:8087) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967) 
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:307)
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:273) 
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:182) 
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:151) 
at com.nitkkr.sanjay.notes.homeScreen.MainActivity.setViewModel(MainActivity.kt:29) 
at com.nitkkr.sanjay.notes.homeScreen.MainActivity.onCreate(MainActivity.kt:17) 
at android.app.Activity.performCreate(Activity.java:8157) 
at android.app.Activity.performCreate(Activity.java:8129) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3509) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3708) 
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:2143) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:236) 
at android.app.ActivityThread.main(ActivityThread.java:8087) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967) 
Caused by: java.lang.RuntimeException: cannot find implementation for com.nitkkr.sanjay.notes.data.database.NoteDatabase. NoteDatabase_Impl does not exist
at androidx.room.Room.getGeneratedImplementation(Room.java:100)
at androidx.room.RoomDatabase$Builder.build(RoomDatabase.java:1486)
at com.nitkkr.sanjay.notes.data.database.NoteDatabase$Companion.getInstance(NoteDatabase.kt:24)
at com.nitkkr.sanjay.notes.data.database.NoteRepository.<init>(NoteRepository.kt:11)
at com.nitkkr.sanjay.notes.homeScreen.HomeScreenVM.<init>(HomeScreenVM.kt:12)
at java.lang.reflect.Constructor.newInstance0(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:307) 
at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:273) 
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:182) 
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:151) 
at com.nitkkr.sanjay.notes.homeScreen.MainActivity.setViewModel(MainActivity.kt:29) 
at com.nitkkr.sanjay.notes.homeScreen.MainActivity.onCreate(MainActivity.kt:17) 
at android.app.Activity.performCreate(Activity.java:8157) 
at android.app.Activity.performCreate(Activity.java:8129) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1310) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3509) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3708) 
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:2143) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:236) 
at android.app.ActivityThread.main(ActivityThread.java:8087) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967) 
viewModel =ViewModelProvider(
this,
HomeScreenViewModelFactory(application)
).get(HomeScreenVM::class.java)
Use this i hope it will work.

ClassNotFoundException - Didn't find class but class referenced is a method?

I'm using build tools 30.0.3 and API level 30, with multidex enabled and imported in gradle
I have an Activity which is declared in my manifest like so
<activity
android:name="com.weare8.android.ui.sponsorHub.watchAndRespond.AWatchAndRespondCongrats"
android:screenOrientation="portrait"
android:theme="#style/App8Theme.noActionBar"/>
And specify the same class at the top of the Activity's file:
package com.weare8.android.ui.sponsorHub.watchAndRespond
I then have a method called "initData" that is called in the Activity's onResume method. The app crashes when it is called with the following stack trace:
2021-03-09 13:35:30.362 25909-25909/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.eight.app.stg, PID: 25909
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/weare8/android/ui/sponsorHub/watchAndRespond/AWatchAndRespondCongrats$initData$1$1;
at com.weare8.android.ui.sponsorHub.watchAndRespond.AWatchAndRespondCongrats.initData(AWatchAndRespondCongrats.kt:71)
at com.weare8.android.ui.sponsorHub.watchAndRespond.AWatchAndRespondCongrats.onResume(AWatchAndRespondCongrats.kt:51)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1456)
at android.app.Activity.performResume(Activity.java:8135)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4428)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4470)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
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:7660)
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.ClassNotFoundException: Didn't find class "com.weare8.android.ui.sponsorHub.watchAndRespond.AWatchAndRespondCongrats$initData$1$1" on path: DexPathList[[zip file "/data/app/~~VZbIKt-wdKVMzrVi50UpkQ==/com.eight.app.stg-NFHvGS3I4QjJpJmMcbLfLw==/base.apk"],nativeLibraryDirectories=[/data/app/~~VZbIKt-wdKVMzrVi50UpkQ==/com.eight.app.stg-NFHvGS3I4QjJpJmMcbLfLw==/lib/arm64, /data/app/~~VZbIKt-wdKVMzrVi50UpkQ==/com.eight.app.stg-NFHvGS3I4QjJpJmMcbLfLw==/base.apk!/lib/arm64-v8a, /system/lib64, /system_ext/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.weare8.android.ui.sponsorHub.watchAndRespond.AWatchAndRespondCongrats.initData(AWatchAndRespondCongrats.kt:71) 
at com.weare8.android.ui.sponsorHub.watchAndRespond.AWatchAndRespondCongrats.onResume(AWatchAndRespondCongrats.kt:51) 
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1456) 
at android.app.Activity.performResume(Activity.java:8135) 
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4428) 
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4470) 
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52) 
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
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:7660) 
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) 
Invalidate cache/Restart does not work, deleting the .idea folder does not work, Clean/rebuild project does not work. I've seen this error before but not for a specific method. I have no idea why this stopped working, seemingly spontaneously. What else can I try?
Edit: crashing method:
private fun initData(offer: WatchAndRespondOffer) = with (offer) {
Interactors.preferences.removedCachedOffer(offer.campaignId!!)
if (!offer.linkToProductOrDiscountIncentive?.shortInstruction.isNullOrEmpty()) {
offerTv.visibility = View.VISIBLE
offerTv.text = offer.linkToProductOrDiscountIncentive?.shortInstruction
}
val imageToBeDisplayed = if (!offer.ctaImageUri.isNullOrEmpty()) {
offer.ctaImageUri
} else {
offer.coverTileUri
}
if (cashIncentive != null && charityDonationIncentive != null) {
title_description.setText(R.string.watch_and_respond_congrats_description)
title_first.show()
title_second.show()
} else if (charityDonationIncentive != null) {
title_second.hide()
title_first.setText(R.string.watch_and_respond_congrats_title_charity)
title_description.setText(R.string.watch_and_respond_congrats_donation_description)
} else if (cashIncentive != null) {
title_second.hide()
title_first.setText(R.string.watch_and_respond_congrats_title_cash)
title_description.setText(R.string.watch_and_respond_congrats_description)
}
val isUnite = offer.isUniteIncentive?: false
if (isUnite){
title_first.hide()
title_second.hide()
title_description.hide()
wellDoneTv.show()
} else{
wellDoneTv.hide()
}
setupLinkIncentive(offer)
Glide.with(applicationContext).asBitmap().load(imageToBeDisplayed).into(object : SimpleTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
val width = Resources.getSystem().displayMetrics.widthPixels - 40
val widthRatio = resource.width
val heightRatio = resource.height.toFloat()
val height = ((width.toFloat() / widthRatio.toFloat()) * heightRatio).toInt()
tileIv.layoutParams.height = height
Glide.with(applicationContext).load(imageToBeDisplayed).apply(RequestOptions().override(width, height).skipMemoryCache(true)).into(tileIv)
}
})
}

Cannot create an instance of class ViewModel in Android MVVM

I am learning android MVVM pattern android development. I wanted to create a recycclerview activity to show data from server. But it's not accepting my viewmodel class. Bellow is my logcat errors.
LOGCAT
2020-04-12 21:03:18.026 2008-2008/com.shovon.mvvm E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.shovon.mvvm, PID: 2008
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shovon.mvvm/com.shovon.mvvm.activity.RecyclerActivity}: java.lang.RuntimeException: Cannot create an instance of class com.shovon.mvvm.viewmodel.RecyclerActivityViewModel
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.shovon.mvvm.viewmodel.RecyclerActivityViewModel
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.shovon.mvvm.activity.RecyclerActivity.onCreate(RecyclerActivity.kt:23)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
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:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
**Caused by: java.lang.InstantiationException: java.lang.Class<com.shovon.mvvm.viewmodel.RecyclerActivityViewModel> 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.shovon.mvvm.activity.RecyclerActivity.onCreate(RecyclerActivity.kt:23) 
at android.app.Activity.performCreate(Activity.java:7802) 
at android.app.Activity.performCreate(Activity.java:7791) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
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:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
VIEWMODEL
Here is my viewmodel class. I am passing some data to activity from this viewmodel. But problem is activity does not accept this viewmodel. Maybe viewmodel has any issue.
class RecyclerActivityViewModel(application: Application) : AndroidViewModel(application) {
val repository = RecyclerActivityRepository(application)
fun userData(): LiveData<ArrayList<UserResponse>>{
return repository.getUserData()
}
}
REPOSITORY
class RecyclerActivityRepository(application: Application) {
var liveData = MutableLiveData<ArrayList<UserResponse>>()
private val userList = ArrayList<UserResponse>()
private var adapter: UserAdapter? = null
fun getUserData(): MutableLiveData<ArrayList<UserResponse>>{
if (liveData == null){
liveData = MutableLiveData<ArrayList<UserResponse>>()
}
RetrofitClient.instance.getUsers()
.enqueue(object : Callback<List<UserResponse>> {
override fun onFailure(call: Call<List<UserResponse>>, t: Throwable) {
Log.v("Retrofit", t.message.toString())
}
override fun onResponse(
call: Call<List<UserResponse>>,
response: Response<List<UserResponse>>
) {
Log.v("Retrofit", "Success")
Log.v("response_code", response.code().toString())
try {
userList.addAll(response.body()!!)
adapter?.notifyDataSetChanged()
}catch (e: Exception){
Log.v("error", e.message.toString())}
}
})
return liveData
}
}
ACTIVITY
Here is my logic code
class RecyclerActivity : AppCompatActivity() {
private lateinit var viewModel: RecyclerActivityViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_recycler)
viewModel = ViewModelProvider(this).get(RecyclerActivityViewModel::class.java)
}
}
this might help
viewModel= ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(RecyclerActivityViewModel::class.java)

java.lang.RuntimeException: Unable to destroy activity {MainActivity}: java.util.ConcurrentModificationException

When I set my application to background and open it again I have a crash:
java.lang.RuntimeException: Unable to destroy activity {MainActivity}: java.util.ConcurrentModificationException
I'm using navigation graph scope for storing viewmodel
fun <T : ViewModel> getViewModelForGraph(
graphId: Int,
clazz: Class<T>,
fragment: Fragment
): T {
val navController = NavHostFragment.findNavController(fragment)
val vmFactory = object : ViewModelProvider.Factory {
override fun <U : ViewModel?> create(modelClass: Class<U>): U =
Factory.getComponent(clazz) as U
}
return ViewModelProvider(
navController.getViewModelStoreOwner(graphId),
vmFactory
).get(clazz)
}
and I'm getting my viewmodel from fragment this way:
getViewModelForGraph(
R.id.my_sub_graph,
MyViewModel::class.java,
this
)
.getViewModelForGraph(
R.id.mySubGraph,
MyViewModel::class.java,
this
)
And problem resolves when I'm getViewModelForGraph and just attaching my viewmodel to the activity.
Am I using navGraph viewmodel storage in a wrong way?
full stackTrace
java.lang.RuntimeException: Unable to destroy activity {app/MainActivity}: java.util.ConcurrentModificationException
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4941)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4970)
at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:44)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextNode(HashMap.java:1441)
at java.util.HashMap$KeyIterator.next(HashMap.java:1465)
at androidx.navigation.NavControllerViewModel.onCleared(NavControllerViewModel.java:62)
at androidx.lifecycle.ViewModel.clear(ViewModel.java:139)
at androidx.lifecycle.ViewModelStore.clear(ViewModelStore.java:62)
at androidx.fragment.app.FragmentManagerViewModel.clearNonConfigState(FragmentManagerViewModel.java:160)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1026)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
at androidx.fragment.app.FragmentManagerImpl.dispatchDestroy(FragmentManagerImpl.java:2644)
at androidx.fragment.app.FragmentController.dispatchDestroy(FragmentController.java:329)
at androidx.fragment.app.FragmentActivity.onDestroy(FragmentActivity.java:366)
at androidx.appcompat.app.AppCompatActivity.onDestroy(AppCompatActivity.java:210)
at android.app.Activity.performDestroy(Activity.java:8048)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1341)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4926)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4970) 
at android.app.servertransaction.DestroyActivityItem.execute(DestroyActivityItem.java:44) 
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

Categories

Resources