我是Dagger 2的新手。我已经尝试过设置相同的内容。这是我的 ActivityModule :
@Module
public abstract class ActivityModule {
@ContributesAndroidInjector
abstract ProductListActivity contributeProductListActivity();
@ContributesAndroidInjector
abstract ProductDetailsActivity contributeProductDetailsActivity();
}
我的 AppModule :
@Module
class AppModule {
@Provides
@Singleton
RedMartProductService provideRedMartProductService() {
........
}
........
.......
}
我的 AppComponent :
@Singleton
@Component(modules = {AndroidInjectionModule.class, ActivityModule.class, AppModule.class})
public interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
AppComponent build();
}
void inject(MartApplication martApp);
}
我的申请:
public class MartApplication extends Application implements HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
@Override
public void onCreate() {
super.onCreate();
AppInjector.init(this);
}
@Override
public DispatchingAndroidInjector<Activity> activityInjector() {
return dispatchingAndroidInjector;
}
}
在 AppInjector 中:
public class AppInjector {
private AppInjector() {
}
public static void init(MartApplication application) {
DaggerAppComponent.builder().application(application)
.build().inject(application);
application
.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
handleActivity(activity);
}
............
});
}
private static void handleActivity(Activity activity) {
if (activity instanceof Injectable) { //HasSupportFragmentInjector) {
AndroidInjection.inject(activity);
}
if (activity instanceof FragmentActivity) {
((FragmentActivity) activity).getSupportFragmentManager()
.registerFragmentLifecycleCallbacks(
new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentCreated(FragmentManager fm, Fragment f,
Bundle savedInstanceState) {
if (f instanceof Injectable) {
AndroidSupportInjection.inject(f);
}
}
}, true);
}
}
}
注入时会抛出异常:
java.lang.RuntimeException: Unable to create application com.kaushik.myredmart.MartApplication: java.lang.IllegalStateException: com.kaushik.myredmart.di.AppModule must be set at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4710)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
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.IllegalStateException: com.mymart.di.AppModule must be set
at com.kaushik.myredmart.di.DaggerAppComponent$Builder.build(DaggerAppComponent.java:180)
at com.kaushik.myredmart.di.AppInjector.init(AppInjector.java:30)
at com.kaushik.myredmart.MartApplication.onCreate(MartApplication.java:28)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1013)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4707)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
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)
任何人都可以帮助我了解我的设置中究竟出现的问题吗?
答案 0 :(得分:3)
我相信必须在AppComponent的Builder界面中设置AppModule
,并且应该在创建AppComponent时提供。{/ p>
<强> AppComponent:强>
@Singleton
@Component(modules = {AndroidInjectionModule.class, ActivityModule.class, AppModule.class})
public interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
Builder appModule(AppModule appModule); // add this
AppComponent build();
}
void inject(MartApplication martApp);
}
<强> AppInjector:强>
内部init
方法:
DaggerAppComponent.builder()
.application(application)
.appModule(new AppModule()) // add this
.build()
.inject(application);
答案 1 :(得分:1)
您需要将@ContributesAndroidInjector
添加到ActivityModule
@Module
public abstract class ActivityModule {
@ContributesAndroidInjector
abstract ProductListActivity contributeProductListActivity();
@ContributesAndroidInjector
abstract ProductDetailsActivity contributeProductDetailsActivity();
}
答案 2 :(得分:0)
你的整个结构似乎有些奇怪。使用所有这些复杂功能的任何特殊原因(记住你说你是Dagger的新手,并且它的所有教程都没有任何接近你的实现)?