You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Dagger Hilt注入Fragment时应用崩溃,启动Activity失败求助

问题:Fragment注入Dialog时应用崩溃

此前在ViewModel中进行Room等注入操作一切正常,现在尝试为Fragment注入NewContactDialog时应用崩溃。

Fragment代码

@AndroidEntryPoint
class ContactListFragment @Inject constructor(private val newContactDialog: NewContactDialog) :
    Fragment() {

}

Module代码

@Module
@InstallIn(SingletonComponent::class)
object ContactModule {

    @Provides
    @Singleton
    fun provideNewContactDialog(): NewContactDialog {
        return NewContactDialog()
    }

    @Provides
    @Singleton
    fun provideContactListFragment(contactDialog: NewContactDialog): ContactListFragment {
        return ContactListFragment(contactDialog)
    }
}

错误日志

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.roomcontactlist/com.example.roomcontactlist.MainActivity}: android.view.InflateException: Binary XML file line #18 in com.example.roomcontactlist:layout/activity_main: Binary XML file line #18 in com.example.roomcontactlist:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView

MainActivity布局XML

第18行对应app:navGraph="@navigation/main_navgraph":

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/main_navgraph" />

</androidx.constraintlayout.widget.ConstraintLayout>

解决方案

错误原因

  1. 同时使用@AndroidEntryPoint注解和在Module中手动提供ContactListFragment实例,与Navigation组件的Fragment创建逻辑冲突——Navigation需要Hilt自动管理Fragment实例化,手动提供单例Fragment会触发InflateException。
  2. NewContactDialog无参构造不符合Android组件依赖规则,Dialog必须依赖Context才能创建,直接无参实例化会导致上下文缺失问题。

修复步骤

  1. 删除Module中提供Fragment的方法
    移除ContactModule里的provideContactListFragment方法,让@AndroidEntryPoint全权负责Fragment的实例注入:
@Module
@InstallIn(SingletonComponent::class)
object ContactModule {

    @Provides
    @Singleton
    fun provideNewContactDialog(@ApplicationContext context: Context): NewContactDialog {
        return NewContactDialog(context)
    }
}
  1. 修正NewContactDialog的构造
    让Dialog接收Context依赖,可选择两种方式:
// 方式1:使用@Inject构造函数
class NewContactDialog @Inject constructor(
    @ApplicationContext private val context: Context
) : Dialog(context) {
    // 你的Dialog逻辑
}

// 方式2:保留Module中的provide方法(已在步骤1修改),通过传入ApplicationContext创建Dialog
  1. 保持Fragment的注入配置
    原Fragment的@AndroidEntryPoint和@Inject构造函数无需修改,Hilt会自动注入NewContactDialog实例。

完成以上修改后,Navigation组件可正常创建Fragment实例,注入逻辑恢复正常,避免InflateException崩溃。

内容的提问来源于stack exchange,提问作者Electrocode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 06:17:36