DispatchingActivityInjector返回null

时间:2018-01-02 22:49:14

标签: android dagger-2 dagger

好的,所以我已经在这个问题上打了很长时间。由于我的活动中的AndroidInjection.inject(this)上的NPE,我的应用程序几乎立即失败...问题似乎是DispatchingActivityInjector(注入)返回null但我不知道我哪里出错了。

无论如何,这是我所有的DI内容:

Appcomponent:

@Component(modules = {
        ImageCaptureModule.class,
        InjectorsModule.class
})
public interface AppComponent {
    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(Application application);

        AppComponent build();
    }

    void inject(Application application);
}

的AppModule:

@Module
public class AppModule {
    private Context context;

    public AppModule(@NonNull Application application) {
        this.context = application;
    }
}

InjectorModule:

@Module(includes = AndroidSupportInjectionModule.class)
public abstract class InjectorsModule {
    @ActivityScope
    @ContributesAndroidInjector(modules = {ImageCaptureModule.class})
    abstract ImageCaptureActivity contributeImageCaptureActivity();
}

ImageCaptureModule:

@Module
public abstract class ImageCaptureModule {}

应用:

@Inject DispatchingAndroidInjector<Activity> dispatchingActivityInjector;

@Override
public void onCreate() {
    DaggerAppComponent.builder().application(this).build().inject(this);
    super.onCreate();
}

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

的活动:

public class ImageCaptureActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AndroidInjection.inject(this); // this is where the failure occurs

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_capture);

        ImageCaptureViewModel viewModel = ViewModelProviders.of(this, super.viewModelFactory).get(ImageCaptureViewModel.class);
    }
}

这是堆栈跟踪:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ImageCaptureActivity}: java.lang.NullPointerException: mApplication.activityInjector() returned null
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2421)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481)
                                                                         at android.app.ActivityThread.access$900(ActivityThread.java:155)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:174)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5440)
                                                                         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.NullPointerException: Application.activityInjector() returned null
                                                                         at dagger.internal.Preconditions.checkNotNull(Preconditions.java:83)
                                                                         at dagger.android.AndroidInjection.inject(AndroidInjection.java:57)
                                                                         at ImageCaptureActivity.onCreate(ImageCaptureActivity.java:15)
                                                                         at android.app.Activity.performCreate(Activity.java:6285)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2374)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2481) 
                                                                         at android.app.ActivityThread.access$900(ActivityThread.java:155) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:174) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5440) 
                                                                         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) 

2 个答案:

答案 0 :(得分:0)

好的,所以我稍微切换了应用程序的DI部分以修复错误,如果有人遇到同样的问题,这对我有用(对Dagger 2.14.1很好)

@Component(modules = {
        InjectorsModule.class,
        ImageCaptureModule.class,
        AndroidInjectionModule.class
})
public interface AppComponent extends AndroidInjector<MyApplication> {
    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(Application application);

        AppComponent build();
    }

    void inject(Application application);
}

的AppModule:

@Module
public class AppModule {
    private Context context;

    public AppModule(@NonNull Application application) {
        this.context = application;
    }

    @Provides
    @Singleton
    Context provideContext() {
        return context;
    }

    @Provides
    @Singleton
    CameraManager provideCameraManager() {
        return new CameraManager();
    }
}

活动注射器:

@Module(includes = AndroidInjectionModule.class)
public abstract class InjectorsModule {
    @ContributesAndroidInjector(modules = {ImageCaptureModule.class})
    @ActivityScope
    abstract ImageCaptureActivity contributeImageCaptureActivity();
}

ImageCaptureModule:

@Module
public abstract class ImageCaptureModule {}

答案 1 :(得分:0)

我也已经为此花了很长时间

您的人

@Component(modules = {
    ImageCaptureModule.class,
    InjectorsModule.class
})
public interface AppComponent {
    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(Application application);

        AppComponent build();
    }

    void inject(Application application);
}

请改为注入Application类更改以注入您的应用程序类(例如TestApplication)

@Component(modules = {
    ImageCaptureModule.class,
    InjectorsModule.class
})
public interface AppComponent {
    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(Application application);

        AppComponent build();
    }

    void inject(**TestApplication** application);
}