【问题标题】:Unable to inject dependency in fragment using dagger2 in Java Android无法在 Java Android 中使用 dagger2 在片段中注入依赖项
【发布时间】:2018-11-29 10:17:09
【问题描述】:

我能够在活动中注入依赖关系并且它工作正常。但是在片段中调用相同的依赖项并没有访问权限。

这就是我所做的。

App.Java

public class App extends MultiDexApplication implements HasActivityInjector, HasFragmentInjector {

    @Inject
    DispatchingAndroidInjector<Activity> activityInjector;

    @Inject
    DispatchingAndroidInjector<Fragment> fragmentInjector;

    private AppComponent mComponent;

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

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

    @Override
    public AndroidInjector<Fragment> fragmentInjector() {
    return fragmentInjector;
    }
}

这是 AppComponent.Java

@Singleton
@Component(modules = { AndroidInjectionModule.class, ActivityModule.class, FragmentModule.class})
public interface AppComponent extends AndroidInjector<App> {

    void inject(App app);

    @Component.Builder
    interface Builder {
    @BindsInstance
    Builder application(Application application);
    AppComponent build();
    }
}

这是 ActivityModule.Java 类

@Module
public abstract class ActivityModule {
    @ContributesAndroidInjector
    abstract MainActivity contributeMainActivityInjector();

    @ContributesAndroidInjector
    abstract MyActivity contributeCampaignActivityInjector();
}

这是 FragmentModule.java 类

@Module
public abstract class FragmentModule {
    @ContributesAndroidInjector
    abstract MyFragment bindMyFragment();
}

现在在我想要注入该依赖项的 MyFragment 类中。在顶部,我在类中添加了这些行

@Inject
ImageDownloaderApi imageDownloaderApi;

然后检查 onCreateView 函数我这样做是为了检查我的依赖项是否像这样工作

boolean injected = imageDownloaderApi == null ? false : true;
    Log.d(this.getClass().getSimpleName(), "Dependency worked: "+ String.valueOf(injected));

而且它每次都返回错误。

相同的代码在活动中起作用。 我在活动中做的另一件事是像这样在 onCreate 下面添加这一行

  @Override
protected void onCreate(Bundle savedInstanceState) {
    AndroidInjection.inject(this);    // this line
    super.onCreate(savedInstanceState);

尝试在片段的 onCreateView 中添加同一行时

AndroidInjection.inject(this);    

它在此下方显示红线。如果我用 getActivity 替换它。它给了我运行时错误。

请看一下并指导我哪里错了?

我为片段做的过程是否正确?因为它显然在活动中发挥作用。

提前感谢您的宝贵时间。

【问题讨论】:

    标签: java android android-fragments dependency-injection dagger-2


    【解决方案1】:

    我假设您使用的是支持库中的Fragment-s。尝试将public class App extends MultiDexApplication implements HasActivityInjector, HasFragmentInjector 更改为public class App extends MultiDexApplication implements HasActivityInjector, HasFragmentInjector, HasSupportFragmentInjector,随后提供适当的注射器。

    This is an easy article 让这个话题变得清晰。另外,HasSupportFragmentInjector接口的用法也在这里介绍。

    【讨论】:

    • 感谢@Andrey 的帮助。这样做时出现此错误 == "java.lang.NullPointerException: com.arteciate.arteciate.di.App.supportFragmentInjector() returned null"
    • 你是否实现了public AndroidInjector&lt;Fragment&gt; supportFragmentInjector(),所以它返回了有效的注入器?
    【解决方案2】:

    尝试在片段的 onCreateView 中添加同一行时

    AndroidInjection.inject(this);

    这应该在您的片段的onAttach 中完成,如果您使用支持库中的片段,正如您应该的那样,该行将是AndroidSupportInjection.inject(this)

    override fun onAttach(context: Context?) {
        AndroidSupportInjection.inject(this)
        super.onAttach(context)
    }
    

    【讨论】:

    • 感谢您的时间 Blackbelt。我忘了在帖子上添加这个。我也试过这个。现在,在您发表评论后,我将 AndroidSupportInjection.inject(this) 添加到 MyFragment 的 onAttach 中。遇到这个问题。
    • java.lang.IllegalArgumentException:在 dagger.android.AndroidSupportInjection.findHasFragmentInjector(AndroidSupportInjection.java:92) 上找不到 com.arteciate.arteciate.fragments.MyFragment 的注入器。 support.AndroidSupportInjection.inject(AndroidSupportInjection.java:57) 在 com.arteciate.arteciate.fragments.HomeArtworksFragment.onAttach(HomeArtworksFragment.java:252) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1363) .....等等
    • HasFragmentInjector 应该在托管片段 iirc 的 Activity 上声明
    猜你喜欢
    • 2019-02-25
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 1970-01-01
    • 2017-09-29
    • 2016-11-16
    相关资源
    最近更新 更多