IllegalStateException: Could not convert SemanticsComponentNode to a View - android

I have a very simple #Composable function that I am trying to use in my layout. However, it ends up throwing an IllegalStateException and I am unable to figure out what I'm missing here.
class RallyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Greeting("Tom")
}
}
}
#Composable
fun Greeting(name: String) {
Text(
"Hello, $name",
TextStyle(fontSize = 48.0F)
)
}
2019-05-09 06:32:33.214 5493-5493/androidx.ui.material.studies E/AndroidRuntime: FATAL EXCEPTION: main
Process: androidx.ui.material.studies, PID: 5493
java.lang.RuntimeException: Unable to start activity ComponentInfo{androidx.ui.material.studies/androidx.ui.material.studies.rally.RallyActivity}: java.lang.IllegalStateException: Could not convert SemanticsComponentNode to a View
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalStateException: Could not convert SemanticsComponentNode to a View
at androidx.compose.ViewApplyAdapter.end(ViewComposer.kt:104)
at androidx.compose.Applier.up(Applier.kt:53)
at androidx.compose.Composer$realizeSlots$10.invoke(Composer.kt:1049)
at androidx.compose.Composer$realizeSlots$10.invoke(Composer.kt:178)
at androidx.compose.Composer$applyChanges$1.invoke(Composer.kt:270)
at androidx.compose.Composer$applyChanges$1.invoke(Composer.kt:178)
at androidx.compose.SlotTable.write(SlotTable.kt:689)
at androidx.compose.Composer.applyChanges(Composer.kt:269)
at androidx.compose.CompositionContext$recompose$1.invoke(CompositionContext.kt:104)
at androidx.compose.CompositionContext$recompose$1.invoke(CompositionContext.kt:24)
at androidx.compose.CompositionContextKt.runWithCurrent(CompositionContext.kt:128)
at androidx.compose.CompositionContext.recompose(CompositionContext.kt:94)
at androidx.compose.Compose.composeInto(Compose.kt:135)
at androidx.compose.ComposeKt.compose(Compose.kt:265)
at androidx.compose.ComposeKt.setContent(Compose.kt:239)
at androidx.ui.material.studies.rally.RallyActivity.onCreate(RallyActivity.kt:46)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

Fixed this issue by wrapping the Greeting composable inside a CraneWrapper and a RallyTheme.
class RallyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CraneWrapper {
RallyTheme {
Greeting("Tom")
}
}
}
}
}

Related

Using ViewModelProvider get with key results in IllegalArgumentException:SavedStateProvider with the given key is already registered

I would like to get separate instances of a ViewModel class from a fragment. I tried
#AndroidEntryPoint
class MyFragment: Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
...
val vm1 = ViewModelProvider(this).get("100", MyViewModel::class.java)
val vm2 = ViewModelProvider(this).get("200", MyViewModel::class.java)
...
//// viewmodel objects are passed downstream to other modules where they
//// will be used
}
}
But I see the crash IllegalArgumentException:SavedStateProvider with the given key is already registered. Using lifecycle version 2.4.1.
AndroidRuntime: java.lang.IllegalArgumentException: SavedStateProvider with the given key is already registered
AndroidRuntime: at androidx.savedstate.SavedStateRegistry.registerSavedStateProvider(SavedStateRegistry.java:111)
AndroidRuntime: at androidx.lifecycle.SavedStateHandleController.attachToLifecycle(SavedStateHandleController.java:50)
AndroidRuntime: at androidx.lifecycle.SavedStateHandleController.create(SavedStateHandleController.java:70)
AndroidRuntime: at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:67)
AndroidRuntime: at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:84)
AndroidRuntime: at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:109)
AndroidRuntime: at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
AndroidRuntime: at com.xxxx.view.MyFragment.onViewCreated(MyFragment.kt:58)
AndroidRuntime: at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3019)
AndroidRuntime: at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:551)
AndroidRuntime: at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:261)
AndroidRuntime: at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1840)
AndroidRuntime: at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1758)
AndroidRuntime: at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1701)
AndroidRuntime: at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2849)
AndroidRuntime: at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2784)
AndroidRuntime: at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:262)
AndroidRuntime: at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:478)
AndroidRuntime: at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
Usage:
What is a correct way to achieve this?
I have the same problem when I go back and again switch to the same activity. I am not using hilt and am using get which should give me the same instance if it already exists.
pruebaViewModel = ViewModelProvider(this, InjectionSingleton.provideViewModelFactory(this)).get("HolaPrueba", PruebaViewModel::class.java)
class ViewModelFactory(
private val savedStateRegistryOwner: SavedStateRegistryOwner,
private val countDownTimerSession: CountDownTimerSession,
private val dataProviderRenta4: DataProviderRenta4,
private val encryptedSharedPreferencesManager: EncryptedSharedPreferencesManager,
private val dataUserSession: DataUserSession,
) : AbstractSavedStateViewModelFactory(savedStateRegistryOwner, null) {
companion object {
var INSTANCE: ViewModelFactory? = null
#Synchronized
fun getInstance(
savedStateRegistryOwner: SavedStateRegistryOwner,
countDownTimerSession: CountDownTimerSession,
dataProviderRenta4: DataProviderRenta4,
encryptedSharedPreferencesManager: EncryptedSharedPreferencesManager,
dataUserSession: DataUserSession
): ViewModelFactory {
if (INSTANCE == null) {
INSTANCE =
ViewModelFactory(savedStateRegistryOwner, countDownTimerSession, dataProviderRenta4, encryptedSharedPreferencesManager, dataUserSession)
}
return INSTANCE!!
}
}
override fun <T : ViewModel> create(key: String, modelClass: Class<T>, savedStateHandle: SavedStateHandle): T {
Log.d(TAG, "l> key key key: $key")
return modelClass.getConstructor(
SavedStateHandle::class.java,
DataProviderRenta4::class.java,
DataUserSession::class.java,
CountDownTimerSession::class.java,
EncryptedSharedPreferencesManager::class.java
).newInstance(savedStateHandle, dataProviderRenta4, dataUserSession, countDownTimerSession, encryptedSharedPreferencesManager)
}
}
2022-07-21 13:29:25.863 22162-22162/com.r4.renta4.des E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.r4.renta4.des, PID: 22162
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.r4.renta4.des/com.r4.renta4.ui.prueba.PruebaActivity}: java.lang.IllegalArgumentException: SavedStateProvider with the given key is already registered
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.IllegalArgumentException: SavedStateProvider with the given key is already registered
at androidx.savedstate.SavedStateRegistry.registerSavedStateProvider(SavedStateRegistry.kt:110)
at androidx.lifecycle.SavedStateHandleController.attachToLifecycle(SavedStateHandleController.java:42)
at androidx.lifecycle.LegacySavedStateHandleController.create(LegacySavedStateHandleController.java:39)
at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:96)
at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:87)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187)
at com.r4.renta4.ui.prueba.PruebaActivity.setupViewModel(PruebaActivity.kt:24)
at com.r4.renta4.ui.base.BaseActivity.onCreate(BaseActivity.kt:149)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

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.

BiometricPrompt crashes the app on rotation / onCreate

I want to use biometric (fingerprint) login as soon as the app launches.
The app uses a hosting activity and loads the login fragment with biometrics (androidx.biometric:biometric:1.2.0-alpha03). Everything regarding the login works fine but as soon as the phone rotates it crashes. From the log file I understand that the crash happens inside the onCreate because it tries to recreate the FingerprintDialogFragment with an empty constructor.
I tried to cancel the authentication in the onPause function and recreate it manually but that doesn't help. So far my code inside the fragment looks like this:
override fun onResume() {
super.onResume()
if(loginSharedPreferences.getBoolean(BIOMETRIC_SET, false)) {
setupBiometricPrompt()
biometricPrompt?.authenticate(promptInfo)
}
}
override fun onPause() {
if(biometricPrompt != null) {
biometricPrompt!!.cancelAuthentication()
biometricPrompt = null
}
super.onPause()
}
private fun setupBiometricPrompt() {
promptInfo = createBiometricPromptInfo()
executor = ContextCompat.getMainExecutor(requireContext())
biometricPrompt = BiometricPrompt(this, executor,
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int,
errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
Log.d(TAG, "Authentication error: $errString")
}
override fun onAuthenticationSucceeded(
result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
Log.d(TAG, "Authentication succeeded!")
callbacks?.loginSuccessful()
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Log.d(TAG, "Authentication failed!")
}
})
}
private fun createBiometricPromptInfo(): BiometricPrompt.PromptInfo {
return BiometricPrompt.PromptInfo.Builder()
.setTitle(resources.getString(R.string.biometric_title))
.setSubtitle(resources.getString(R.string.biometric_subtitle))
.setNegativeButtonText(resources.getString(R.string.biometric_cancel))
.setAllowedAuthenticators(BIOMETRIC_STRONG)
.build()
}
And the log file:
java.lang.RuntimeException: Unable to start activity: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment androidx.biometric.FingerprintDialogFragment: could not find Fragment constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3114)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5039)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4948)
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
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:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Caused by: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment androidx.biometric.FingerprintDialogFragment: could not find Fragment constructor
at androidx.fragment.app.Fragment.instantiate(Fragment.java:628)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57)
at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:483)
at androidx.fragment.app.FragmentStateManager.<init>(FragmentStateManager.java:85)
at androidx.fragment.app.FragmentManager.restoreSaveState(FragmentManager.java:2728)
at androidx.fragment.app.Fragment.restoreChildFragmentState(Fragment.java:1890)
at androidx.fragment.app.Fragment.onCreate(Fragment.java:1867)
at com.tetraguard.android.otp.LoginPinFragment.onCreate(LoginPinFragment.kt:59)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128)
at androidx.fragment.app.FragmentManager.dispatchCreate(FragmentManager.java:3061)
at androidx.fragment.app.FragmentController.dispatchCreate(FragmentController.java:240)
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:276)
at com.tetraguard.android.otp.MainActivity.onCreate(MainActivity.kt:61)
at android.app.Activity.performCreate(Activity.java:7327)
at android.app.Activity.performCreate(Activity.java:7318)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3094)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257) 
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5039) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4948) 
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69) 
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:1948) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7050) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 
Caused by: java.lang.NoSuchMethodException: androidx.biometric.FingerprintDialogFragment.<init> []
at java.lang.Class.getConstructor0(Class.java:2328)
at java.lang.Class.getConstructor(Class.java:1725)
at androidx.fragment.app.Fragment.instantiate(Fragment.java:613)
at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57) 
at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:483) 
at androidx.fragment.app.FragmentStateManager.<init>(FragmentStateManager.java:85) 
at androidx.fragment.app.FragmentManager.restoreSaveState(FragmentManager.java:2728) 
at androidx.fragment.app.Fragment.restoreChildFragmentState(Fragment.java:1890) 
at androidx.fragment.app.Fragment.onCreate(Fragment.java:1867) 
at com.tetraguard.android.otp.LoginPinFragment.onCreate(LoginPinFragment.kt:59) 
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949) 
at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475) 
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278) 
at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:112) 
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1647) 
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3128) 
at androidx.fragment.app.FragmentManager.dispatchCreate(FragmentManager.java:3061) 
at androidx.fragment.app.FragmentController.dispatchCreate(FragmentController.java:240) 
at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:276) 
at com.tetraguard.android.otp.MainActivity.onCreate(MainActivity.kt:61) 
at android.app.Activity.performCreate(Activity.java:7327) 
at android.app.Activity.performCreate(Activity.java:7318) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3094) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257) 
at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5039) 
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4948) 
at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69) 
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:1948) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7050) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 
This issue has been reported to the Google issue tracker. Might be better to follow the discussion here:
https://issuetracker.google.com/issues/181805603

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) 

Proguard rule setup for mvvm

The code is working fine without Proguard Rule setup. But when I enabled proguard in release build, The project is build successfully. App is installed successfully but while launching the app it crashed every time .
Here is the error log
2018-10-29 13:52:04.299 1942-1942/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.eighsquare.mcoopon, PID: 1942
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eighsquare.mcoopon/com.eighsquare.mcoopon.ui.splash.SplashActivity}: java.lang.RuntimeException: Cannot create an instance of class com.eighsquare.mcoopon.ui.splash.a
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.RuntimeException: Cannot create an instance of class com.eighsquare.mcoopon.ui.splash.a
at androidx.lifecycle.v$a.a(ViewModelProvider.java:202)
at androidx.lifecycle.v.a(ViewModelProvider.java:135)
at androidx.lifecycle.v.a(ViewModelProvider.java:103)
at com.eighsquare.mcoopon.base.BaseActivity$b.b(**BaseActivity.kt**:32)
at com.eighsquare.mcoopon.base.BaseActivity$b.a(BaseActivity.kt:16)
at kotlin.o.a(LazyJVM.kt:74)
at com.eighsquare.mcoopon.base.BaseActivity.q(Unknown Source:7)
at com.eighsquare.mcoopon.base.BaseActivity.onCreate(BaseActivity.kt:50)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Caused by: java.lang.NoSuchMethodException: <init> [class android.app.Application]
at java.lang.Class.getConstructor0(Class.java:2320)
at java.lang.Class.getConstructor(Class.java:1725)
at androidx.lifecycle.v$a.a(ViewModelProvider.java:200)
at androidx.lifecycle.v.a(ViewModelProvider.java:135) 
at androidx.lifecycle.v.a(ViewModelProvider.java:103) 
at com.eighsquare.mcoopon.base.BaseActivity$b.b(BaseActivity.kt:32) 
at com.eighsquare.mcoopon.base.BaseActivity$b.a(BaseActivity.kt:16) 
at kotlin.o.a(LazyJVM.kt:74) 
at com.eighsquare.mcoopon.base.BaseActivity.q(Unknown Source:7) 
at com.eighsquare.mcoopon.base.BaseActivity.onCreate(BaseActivity.kt:50) 
at android.app.Activity.performCreate(Activity.java:7009) 
at android.app.Activity.performCreate(Activity.java:7000) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
when I click on the BaseActivity.kt I am redirected to this code
val viewModel by lazy {
if (viewModelFactory != null)
ViewModelProviders.of(this, viewModelFactory).get(mViewModelClass)
else ViewModelProviders.of(this).get(mViewModelClass)
}
This code is placed in the BaseActivity to create Viewmodel.
Here is My BaseActivity.kt
abstract class BaseActivity<VM : ViewModel, DB : ViewDataBinding>(private val mViewModelClass: Class<VM>) : AppCompatBaseActivity() {
#LayoutRes
abstract fun getLayoutRes(): Int
abstract val viewModelFactory: ViewModelProvider.Factory?
val binding by lazy {
DataBindingUtil.setContentView(this, getLayoutRes()) as DB
}
val viewModel by lazy {
if (viewModelFactory != null)
ViewModelProviders.of(this, viewModelFactory).get(mViewModelClass)
else ViewModelProviders.of(this).get(mViewModelClass)
}
/**
* If you want to inject Dependency Injection
* on your activity, you can override this.
*/
open fun onInject() {}
open fun initView() {}
override fun onCreate(savedInstanceState: Bundle?) {
onInject()
LayoutInflaterCompat.setFactory2(layoutInflater, IconicsLayoutInflater2(delegate))
super.onCreate(savedInstanceState)
initViewModel(viewModel)
initView()
}
/**
*
* You need override this method.
* And you need to set viewModel to binding: binding.viewModel = viewModel
*
*/
abstract fun initViewModel(viewModel: VM)
fun isNetworkConnected(): Boolean {
return NetworkUtils.isNetworkConnected(this)
}
fun hideKeyboard() {
Utils.hideKeyboard(this)
}
fun changeFragment(fragment: Fragment, cleanStack: Boolean = false, addToBackStack: Boolean = true) {
Utils.changeFragment(this, fragment, cleanStack, addToBackStack)
}
}
I am using Dagger2
I lost my 2 hrs in searching the solution. Stackoverflow is the last hope.
After a long research I got a solution.
I Simply needed to add public #Inject constructor in my ViewModel class
eg;
class FullScreenViewModel public #Inject constructor(application: Application) : BaseViewModel<Any?>(application) {}

Categories

Resources