android-viewbinding icon indicating copy to clipboard operation
android-viewbinding copied to clipboard

Fatal Exception: java.lang.IllegalStateException

Open glebkravchenko opened this issue 3 years ago • 7 comments

Hi, on some devices when open BottomSheetDialogFragment app got crashed with this exception:

Fatal Exception: java.lang.IllegalStateException Can't access the Fragment View's LifecycleOwner when getView() is null i.e., before onCreateView() or after onDestroyView()

Exception is pointing to viewbindig method: DoWorkoutFragment.getFragmentBinding (DoWorkoutFragment.kt:50) DoWorkoutFragment.access$getFragmentBinding (DoWorkoutFragment.kt:38) DoWorkoutFragment$setupDoWorkoutScreen$1.invokeSuspend (DoWorkoutFragment.kt:79) kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith (ContinuationImpl.kt:33) kotlinx.coroutines.DispatchedTask.run (DispatchedTask.kt:106)

Which is inside of view binding delegate. What do I need to do to avoid this?

glebkravchenko avatar Jan 17 '22 12:01 glebkravchenko

Can you provide how do you import the by viewBinding() is @glebkravchenko?

yogacp avatar Jan 18 '22 03:01 yogacp

What do you mean? Import to the project? Or import inside of the fragment?

glebkravchenko avatar Jan 26 '22 15:01 glebkravchenko

@glebkravchenko, it seems that @yogacp presumed that this is the usual incorrect import from dialog viewbinding to fragment viewbinding. however, this bug happened to us too, and there is nothing wrong with the import

thegarlynch avatar Jun 13 '22 14:06 thegarlynch

It also happened to me with the same error message but it was on Fragment instead of BottomSheetDialogFragment, and I check i imported the correct viewbinding. I think it caused by this line image

rezyfr avatar Jun 13 '22 17:06 rezyfr

@yogacp you might want to see this

https://medium.com/@Zhuinden/an-update-to-the-fragmentviewbindingdelegate-the-bug-weve-inherited-from-autoclearedvalue-7fc0a89fcae1

thegarlynch avatar Jun 14 '22 01:06 thegarlynch

We need a fix for that because it's crashing the app every time when I'm going by saved fragments stack back ...

dajver avatar Jul 13 '22 14:07 dajver

@glebkravchenko @thegarlynch Make sure not to access the view before onCreateView and after onDestroyView.

If you use Flow, you can read this article

I experienced the same thing after I read my code, it turns out that there is something that does set view before onCreateView and after onDestroyView.

Egi10 avatar Jul 14 '22 02:07 Egi10