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

kcwpcxri  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(205)

目前,我正在将我的应用程序从KotlinSynthetic迁移到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 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)

布局文件:

<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文件:

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
}

非常感谢它,如果任何人提供一些指导方针或相关的参考链接。

rqqzpn5f

rqqzpn5f1#

如果要使用ViewBinding,则应在xml文件中 Package ...““标记

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

ppcbkaq52#

我有同样的问题,每次我使用片段作为navost在活动中,花了2天后,现在我已经找到了一个解决方案,这是为我工作。
在超级调用之后,您需要初始化绑定类,如下所示:

super.onCreate(savedInstanceState)
registerBinding = ActivityRegistrationBinding.inflate(layoutInflater)
setContentView(registerBinding?.root)

相关问题