Crash with inflating NavHostFragment
grajadurai28 opened this issue · 2 comments
Hi,
I am using the new Navigation Architecture for Fragment transactions. My home screen has a NavHostFragment
defined in its layout.
For theming I am using the CyaneaThemePickerActivity
. After picking a theme, if I press the back button to navigate back to the home screen, I get this crash. I am unable to find out what the root cause is.
Adding the stack trace below for reference,
Unable to start activity ComponentInfo{com.example.features.home.HomeActivity}: android.view.InflateException: Binary XML file line #17: Binary XML file line #79: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4519)
at android.app.ActivityThread.-wrap19(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.view.InflateException: Binary XML file line #17: Binary XML file line #79: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #79: Error inflating class fragment
Caused by: java.lang.IllegalStateException: Fragment androidx.navigation.fragment.NavHostFragment did not create a view.
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3778)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:120)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:405)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:387)
at com.jaredrummler.cyanea.inflator.CyaneaLayoutInflater$PrivateWrapperFactory2.onCreateView(CyaneaLayoutInflater.kt:210)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:994)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at com.jaredrummler.cyanea.inflator.CyaneaLayoutInflater.inflate(CyaneaLayoutInflater.kt:61)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:303)
at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284)
at com.example.features.home.HomeActivity.initBindingAndViewModel(HomeActivity.kt:220)
at com.example.features.home.HomeActivity.init(HomeActivity.kt:86)
at com.example.features.home.HomeActivity.onCreate(HomeActivity.kt:81)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4519)
at android.app.ActivityThread.-wrap19(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Found the issue. We were using the NavController
to switch between two NavGraph
using the same NavHostFragment
. The default NavGraph
- nav_home
is set in the onCreate
of the Activity
. The other NavGraph
- nav_settings
is set on selecting the Settings option.
The theming logic is part of the nav_settings
graph. So, when cyanea
restarts the Activity
to apply theme, onCreate
gets called and NavController
sets the NavGraph
back to nav_home
.
The NavController
then tries to find the Settings page in nav_home
. Hence, the crash.
The only way around this is to store the current NavGraph
id in ViewModel
or savedInstanceState
and use it in onCreate
to set the correct NavGraph
Will close this issue now. Thanks
I also had this last one, and I would like to have a prototype of correction to solve this problem finally, an example of resolution (pseudo code) you will have already deduced it from my accent I am French-speaking ... all my excuses