在尝试从我们的项目中使用Dagger v2.11的新
Android匕首类时,我遇到了错误.我不确定问题是什么,因为我在
https://google.github.io/dagger//android.html考虑了Android文档并且仍然无法解决这个问题.有关此设置有什么问题的任何想法?
我的应用程序:
public class MyApplication extends Application implements HasActivityInjector { @Inject AndroidInjector<Activity> androidInjector; @Override public void onCreate() { super.onCreate(); AppInjector.init(this); } @Override public AndroidInjector<Activity> activityInjector() { return androidInjector; } }
AppComponent:
@Singleton @Component(modules = { AndroidSupportInjectionModule.class,AppModule.class,ActivityModule.class,}) public interface AppComponent { @Component.Builder interface Builder { @BindsInstance Builder application(Application application); AppComponent build(); } void inject(MyApplication myApplication); }
ActivityModule:
@Module abstract class ActivityModule { @ContributesAndroidInjector(modules = FragmentModule.class) abstract MainActivity contributeMainActivity(); // all other activities defined as above }
FragmentModule:
@Module abstract class FragmentModule { @ContributesAndroidInjector abstract MyFragment contributeMyFragment(); }
AppInjector:
public static void init(MyApplication myApplication) { DaggerAppComponent.builder().application(myApplication) .build().inject(myApplication); myApplication .registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity,Bundle savedInstanceState) { handleActivity(activity); } @Override public void onActivityStarted(Activity activity) { } @Override public void onActivityResumed(Activity activity) { } @Override public void onActivityPaused(Activity activity) { } @Override public void onActivityStopped(Activity activity) { } @Override public void onActivitySaveInstanceState(Activity activity,Bundle outState) { } @Override public void onActivityDestroyed(Activity activity) { } }); } private static void handleActivity(Activity activity) { if (activity instanceof 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); } }
的AppModule:
@Module(includes = viewmodelModule.class) class AppModule { @Singleton @Provides UserDao provideUserDao(MyDatabase db) { return db.userDao(); } // all other DAOs }
viewmodelModule:
@Module abstract class viewmodelModule { @Binds @IntoMap @viewmodelKey(Userviewmodel.class) abstract viewmodel bindUserviewmodel(Userviewmodel userviewmodel); }
主要活动:
public class MainActivity extends AppCompatActivity implements HasSupportFragmentInjector { @Inject AndroidInjector<Fragment> androidInjector; @Override public AndroidInjector<Fragment> supportFragmentInjector() { return androidInjector; } }
堆栈跟踪:
Error:(29,10) error: dagger.android.AndroidInjector<android.app.Activity> cannot be provided without an @Provides- or @Produces-annotated method. dagger.android.AndroidInjector<android.app.Activity> is injected at com.myapp.MyApplication.androidInjector com.myapp.MyApplication is injected at com.myapp.dagger.AppComponent.inject(myApplication)
解决方法
尝试将AndroidInjector替换为androidInjector变量的DispatchingAndroidInjector.
这对我有用.这是我的(kotlin)
这对我有用.这是我的(kotlin)
class PodcastApp : Application(),HasActivityInjector { @Inject lateinit var dispatchingAndroidInjector : DispatchingAndroidInjector<Activity> companion object { lateinit var instance: PodcastApp } override fun onCreate() { super.onCreate() instance = this FirebaseApp.initializeApp(this) if (BuildConfig.DEBUG) { Timber.plant(Timber.DebugTree()) Stetho.initializeWithDefaults(this) AppInjector.init(this) } } override fun activityInjector(): AndroidInjector<Activity> { return dispatchingAndroidInjector }
}