本文介绍了AndroidInjector< android.app.Activity>没有@Provides或@Produces注释的方法无法提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

在尝试从我们的项目中的 Dagger v2.11 获取新的 Android dagger 类时遇到错误。我不确定是什么问题,因为我在,仍然无法解决此问题。关于此设置有什么问题的任何想法?

I'm getting the error while trying to get the new Android dagger classes from Dagger v2.11 working in our project. I'm not exactly sure what the problem is, as I've taken into account the Android documentation over at https://google.github.io/dagger//android.html and still cannot get over this issue. Any ideas on what's wrong with this setup?

我的应用程序:

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);
}

MainActivity:

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 替换为 DispatchingAndroidInjector >您的 androidInjector 变量。
这对我有用。这是我的(kotlin)

Try to replace AndroidInjector to DispatchingAndroidInjector of your androidInjector variable.That's work for me. Here is mine ( 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
}

}

这篇关于AndroidInjector&lt; android.app.Activity&gt;没有@Provides或@Produces注释的方法无法提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 15:19