Android错误“ android.app.Application无法实现dagger.android.HasActivityInjector”

时间:2018-09-25 15:25:48

标签: android dependency-injection dragger

我试图运行我的应用程序,每次我单击一个按钮,它调用特定的活动(ExtendedActivity)时,我都会遇到以下错误:

Process: com.converter.android.dailycope, PID: 28573
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.converter.android.dailycope/com.converter.android.dailycope.ui.extended.ExtendedActivity}: java.lang.RuntimeException: android.app.Application does not implement dagger.android.HasActivityInjector
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
               Caused by: java.lang.RuntimeException: android.app.Application does not implement dagger.android.HasActivityInjector
                  at dagger.android.AndroidInjection.inject(AndroidInjection.java:48)
                  at com.converter.android.dailycope.ui.base.BaseActivity.performDependencyInjection(BaseActivity.java:105)
                  at com.converter.android.dailycope.ui.base.BaseActivity.onCreate(BaseActivity.java:35)
                  at com.converter.android.dailycope.ui.extended.ExtendedActivity.onCreate(ExtendedActivity.java:34)
                  at android.app.Activity.performCreate(Activity.java:6251)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)

错误在第super.onCreate(savedInstanceState)

public class ExtendedActivity extends BaseActivity<ActivityExtendedBinding, ExtendedViewModel> {

@Inject
ViewModelProvider.Factory mViewModelFactory;
ActivityExtendedBinding mActivityStartBinding;
private ExtendedViewModel mExtendedViewModel;

private String caps, horos, tipo;
private String data, prefString;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
(...)

我有HasActivityInjector的类

public class Cope extends Application implements HasActivityInjector {

@Inject
DispatchingAndroidInjector<Activity> activityDispatchingAndroidInjector;


@Override
public void onCreate() {
    super.onCreate();

    DaggerExtendedComponent.builder().application(this).build().inject(this);

    if (BuildConfig.DEBUG) {
        Timber.plant(new Timber.DebugTree());
    }

}


@Override
public AndroidInjector<Activity> activityInjector() {
    return activityDispatchingAndroidInjector;
}

}

你能帮我吗?

4 个答案:

答案 0 :(得分:8)

我解决了在AndroidManifest.xml中添加android:name的问题

答案 1 :(得分:0)

您的应用程序类应实现HasActivityInjector。使用官方documentation正确设置匕首。

答案 2 :(得分:0)

如果实施正确,那么请在清单文件中检查是否已添加/分配了应用标签的名称。

 <application
    android:name=".base.BaseApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"/>

答案 3 :(得分:0)

我确实面临着同样的问题。只需清理并重建项目即可解决问题。