'Android NavHostFragment (fragment) inflation failed with ViewBinding (Using navigation component)
Currently, I am migrating my app to ViewBinding from Kotlin Synthetic. I made all the changes by following the migration guide but after migrating OnboardingActivity. The app is getting crashed when I launch OnboardingActivity. I did google to figure what is missing here but I didn't have any luck so far.
Crash logs:
2021-10-13 12:59:52.057 32296-32296/dev.bipin.sample E/AndroidRuntime: FATAL EXCEPTION: main
Process: dev.bipin.sample, PID: 32296
java.lang.RuntimeException: Unable to start activity ComponentInfo{dev.bipin.sample/com.hammr.ui.onboarding.OnboardingActivity}: android.view.InflateException: Binary XML file line #19 in dev.bipin.sample:layout/activity_onboarding: Binary XML file line #19 in dev.bipin.sample:layout/activity_onboarding: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3271)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410)
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:2017)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7397)
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:935)
Caused by: android.view.InflateException: Binary XML file line #19 in dev.bipin.sample:layout/activity_onboarding: Binary XML file line #19 in dev.bipin.sample:layout/activity_onboarding: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #19 in dev.bipin.sample:layout/activity_onboarding: Error inflating class fragment
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context androidx.fragment.app.FragmentHostCallback.getContext()' on a null object reference
at androidx.fragment.app.FragmentManager$3.instantiate(FragmentManager.java:483)
at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:94)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1069)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:997)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:961)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1123)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1084)
at android.view.LayoutInflater.inflate(LayoutInflater.java:682)
at android.view.LayoutInflater.inflate(LayoutInflater.java:534)
at com.hammr.databinding.ActivityOnboardingBinding.inflate(ActivityOnboardingBinding.java:82)
at com.hammr.databinding.ActivityOnboardingBinding.inflate(ActivityOnboardingBinding.java:76)
at com.hammr.di.module.ui.ViewBindingModule.onboardingActivityBinding(ViewBindingModule.kt:20)
at com.hammr.di.module.ui.ViewBindingModule_OnboardingActivityBindingFactory.onboardingActivityBinding(ViewBindingModule_OnboardingActivityBindingFactory.java:37)
at com.hammr.di.component.DaggerAppComponent$OnboardingActivitySubcomponentImpl.getActivityOnboardingBinding(DaggerAppComponent.java:669)
at com.hammr.di.component.DaggerAppComponent$OnboardingActivitySubcomponentImpl.injectOnboardingActivity(DaggerAppComponent.java:1053)
at com.hammr.di.component.DaggerAppComponent$OnboardingActivitySubcomponentImpl.inject(DaggerAppComponent.java:1047)
at com.hammr.di.component.DaggerAppComponent$OnboardingActivitySubcomponentImpl.inject(DaggerAppComponent.java:543)
at dagger.android.DispatchingAndroidInjector.maybeInject(DispatchingAndroidInjector.java:113)
at dagger.android.DispatchingAndroidInjector.inject(DispatchingAndroidInjector.java:134)
at dagger.android.AndroidInjection.inject(AndroidInjection.java:181)
2021-10-13 12:59:52.058 32296-32296/dev.bipin.sample E/AndroidRuntime: at dagger.android.AndroidInjection.inject(AndroidInjection.java:55)
at dagger.android.support.DaggerAppCompatActivity.onCreate(DaggerAppCompatActivity.java:52)
at com.hammr.ui.base.ViewBindingBaseActivity.onCreate(ViewBindingBaseActivity.kt:18)
at com.hammr.ui.onboarding.OnboardingActivity.onCreate(OnboardingActivity.kt:29)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1300)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3246)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410)
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:2017)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7397)
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:935)
Layout file:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/onboardingRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.onboarding.OnboardingActivity">
<fragment
android:id="@+id/navOnboardingHost"
class="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/onboardingStepTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_16sdp"
android:text="@string/progress_step_count"
android:textColor="@color/onboarding_step_counter_text_color"
android:textSize="@dimen/_10ssp"
app:layout_constraintBottom_toTopOf="@id/onboardingStepProgressIndicator"
app:layout_constraintStart_toEndOf="@id/onboardingVerticalStartGuidelines"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread"
tools:text="Step 1 of 4" />
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/onboardingStepProgressIndicator"
android:layout_width="@dimen/_154sdp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_6sdp"
android:layout_marginBottom="@dimen/_8sdp"
android:progress="25"
app:indicatorColor="@color/onboarding_step_progress_track_color"
app:layout_constraintStart_toEndOf="@id/onboardingVerticalStartGuidelines"
app:layout_constraintTop_toBottomOf="@id/onboardingStepTextView"
app:layout_constraintVertical_chainStyle="packed"
app:trackColor="@color/onboarding_step_progress_remaining_track_color"
app:trackCornerRadius="@dimen/_16sdp"
app:trackThickness="@dimen/_3sdp" />
<!--- Other UI components -->
</androidx.constraintlayout.widget.ConstraintLayout>
Kotlin files:
abstract class ViewBindingBaseActivity<VB : ViewBinding> :
DaggerAppCompatActivity() {
@Inject
lateinit var binding: VB
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
}
// Other codes
}
class OnboardingActivity : ViewBindingBaseActivity<ActivityOnboardingBinding>() {
@Inject
lateinit var onboardingActivityViewModel: OnboardingActivityViewModel
@Inject
lateinit var userProfileViewModel: UserProfileViewModel
// Other code
}
Greatly appreciate it if anyone provide some guidelines or related references link.
Solution 1:[1]
If you want to use ViewBinding You should wrapping in your xml file "<layout> ...</layout>" tag
<layout>
<androidx.constraintlayout.widget.ConstraintLayout
....
tools:context=".ui.onboarding.OnboardingActivity">
...
</layout>
Solution 2:[2]
I have the same issue every time I used fragment as a navost in activity, After spending 2 days now I have found a solution which is working for me.
You need to initialise the binding class after super call like this:
super.onCreate(savedInstanceState)
registerBinding = ActivityRegistrationBinding.inflate(layoutInflater)
setContentView(registerBinding?.root)
Solution 3:[3]
The code example you've given is perhaps a poor example of what you're trying to achieve, but I think I have the gist of it.
Actions, like all delegate types, are closures that encapsulate both a method reference and a reference to an object instance - although for closures over static methods that instance reference may be null. Once you create an instance of a closure there's no way to modify the object it refers to. If you're expecting that myClass will change then you'll have to take a more indirect approach, using a method that references the myClass field in Class1:
Class3.Run(() => myClass.WriteText("Hi"));
The lambda expression will be called when the 'some code that causes a 60 second delay' has completed, and it will fetch the current value of myClass at that point in time. You'll have to change myClass to be a static field to access it without a reference to an instance of Class1 of course, otherwise that won't compile either. And you'll have to set myClass somewhere in the process or it'll error on that line.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | HealthyRyu |
| Solution 2 | blackgreen |
| Solution 3 | Corey |
