Android NavHostFragment(片段)膨胀失败,ViewBinding(使用导航组件)

Posted

技术标签:

【中文标题】Android NavHostFragment(片段)膨胀失败,ViewBinding(使用导航组件)【英文标题】:Android NavHostFragment (fragment) inflation failed with ViewBinding (Using navigation component) 【发布时间】:2021-12-01 17:27:03 【问题描述】:

目前,我正在将我的应用程序从 Kotlin Synthetic 迁移到 ViewBinding。我按照迁移指南进行了所有更改,但在迁移 OnboardingActivity 之后。当我启动 OnboardingActivity 时,该应用程序崩溃了。我做了谷歌来弄清楚这里缺少什么,但到目前为止我没有任何运气。

崩溃日志:

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 ComponentInfodev.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)

布局文件:

<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_
    android:layout_
    tools:context=".ui.onboarding.OnboardingActivity">

    <fragment
        android:id="@+id/navOnboardingHost"
        class="androidx.navigation.fragment.NavHostFragment"
        android:layout_
        android:layout_
        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_
        android:layout_
        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_
        android:layout_
        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 文件:

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

如果有人提供一些指南或相关参考链接,将不胜感激。

【问题讨论】:

【参考方案1】:

如果你想使用 ViewBinding 你应该在你的 xml 文件中包装“ ...”标签

<layout>
  <androidx.constraintlayout.widget.ConstraintLayout 
    ....
    tools:context=".ui.onboarding.OnboardingActivity">
...
</layout>

【讨论】:

我现在只做 ViewBinding 并且 标签是 DataBinding 的强制标签,请看这个博客:betterprogramming.pub/… 好像是类似的问题。 ***.com/a/15137231/3897810 我刚查了一下,似乎是另一个问题。

以上是关于Android NavHostFragment(片段)膨胀失败,ViewBinding(使用导航组件)的主要内容,如果未能解决你的问题,请参考以下文章

Android 视图绑定:从包含的 xml 布局访问 NavHostFragment 时

Android NavHostFragment(片段)膨胀失败,ViewBinding(使用导航组件)

如何获取 NavHostFragment

Android:安卓学习笔记之navigation的简单理解和使用

Android:安卓学习笔记之navigation的简单理解和使用

Android:安卓学习笔记之navigation的简单理解和使用