无法启动 activity ComponentInfo{app.myProject/app.myProject.ui.screens.home.MainActivity}:java.lang.IllegalStateException

Unable to start activity ComponentInfo{app.myProject/app.myProject.ui.screens.home.MainActivity}: java.lang.IllegalStateException

运行 apk 在 运行 时应用程序崩溃。

Logcat error log

 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: app.myProject, PID: 23589
    java.lang.RuntimeException: Unable to start activity ComponentInfo{app.myProject/app.myProject.ui.screens.home.MainActivity}: java.lang.IllegalStateException: Hilt Activity must be attached to an @HiltAndroidApp Application. Did you forget to specify your Application's class name in your manifest's <application />'s android:name attribute?
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3430)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: java.lang.IllegalStateException: Hilt Activity must be attached to an @HiltAndroidApp Application. Did you forget to specify your Application's class name in your manifest's <application />'s android:name attribute?
        at dagger.hilt.android.internal.managers.ActivityComponentManager.createComponent(ActivityComponentManager.java:76)
        at dagger.hilt.android.internal.managers.ActivityComponentManager.generatedComponent(ActivityComponentManager.java:66)
        at app.myProject.ui.screens.home.Hilt_MainActivity.generatedComponent(Hilt_MainActivity.java:42)
        at app.myProject.ui.screens.home.Hilt_MainActivity.inject(Hilt_MainActivity.java:64)
        at app.myProject.ui.screens.home.Hilt_MainActivity.onContextAvailable(Hilt_MainActivity.java:35)
        at androidx.activity.contextaware.ContextAwareHelper.dispatchOnContextAvailable(ContextAwareHelper.java:99)
        at androidx.activity.ComponentActivity.onCreate(ComponentActivity.java:352)
        at androidx.fragment.app.FragmentActivity.onCreate(FragmentActivity.java:217)
        at app.myProject.core.BaseActivity.onCreate(BaseActivity.kt:27)
        at android.app.Activity.performCreate(Activity.java:7458)
        at android.app.Activity.performCreate(Activity.java:7448)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3409)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199) 
        at android.os.Handler.dispatchMessage(Handler.java:112) 
        at android.os.Looper.loop(Looper.java:216) 
        at android.app.ActivityThread.main(ActivityThread.java:7625) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

已经有一个应用程序 class,它有注释 @HiltAndroidApp 。并将其添加到清单文件中,如下所示:-

Manifest file

 <application
    android:name=".App"
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon="@drawable/ic_app_icon"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme.ph"
    android:usesCleartextTraffic="true"
    tools:ignore="LockedOrientationActivity">

App.kt file

@HiltAndroidApp
class App : android.app.Application() {

override fun onCreate() {
    super.onCreate()
    if (BuildConfig.DEBUG) {
        Timber.plant(Timber.DebugTree())
    }
    instance = this
    setupServices()

}

private fun setupServices() {
    NetworkService.instance.initializeWithApplicationContext(this)
}

companion object {
    private var instance: App? = null

    @JvmStatic
    fun getInstance(): 
private fun setupServices() {
    NetworkService.instance.initializeWithApplicationContext(this)
}

companion object {
    private var instance: App? = null

    @JvmStatic
    fun getInstance(): App? {
        if (instance == null) {
            instance = App()
        }
        return instance
    }
}? {
        if (instance == null) {
            instance = App()
        }
        return instance
    }
}
}

Dependency i used

implementation("com.google.dagger:hilt-android:2.41")
kapt("com.google.dagger:hilt-android-compiler:2.41")

应用程序在安装后立即崩溃并抛出上述奇怪的错误。 我试图找到这个问题,但没有找到正确的解决方案。

谁能告诉我实际问题是什么以及解决这个问题的正确方法是什么。

确保将 @AndroidEntryPoint 添加到 MainActivity 文件中。

还将此依赖项添加到 build.gradle (app) 文件。

kapt("androidx.hilt:hilt-compiler:1.0.0")

像这样->

**//Hilt
implementation("com.google.dagger:hilt-android:2.41")
kapt("com.google.dagger:hilt-android-compiler:2.41")
kapt("androidx.hilt:hilt-compiler:1.0.0")**

现在同步项目,对我来说效果很好。