无法在Java Android中使用dagger2在片段中注入依赖

3

我可以在activity中注入依赖项并且工作正常,但是在fragment中调用同一依赖项却无法访问。

以下是我的操作:

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

每次都返回false。

而在活动中相同的代码可以正常工作。 还有一件事,我在活动中像下面这样在onCreate之后添加了这一行:

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

当尝试在片段的onCreateView方法中添加相同的行时

AndroidInjection.inject(this);    

下方有红色线,如果我使用getActivity替换这个,会出现运行时错误。

请检查并指导我哪里出错了?

我对Fragment的处理方式是否正确?因为在活动中它明显是有效的。

提前感谢您的时间。

2个回答

3
我假设您正在使用来自支持库的“Fragment”。请尝试将 public class App extends MultiDexApplication implements HasActivityInjector, HasFragmentInjector 更改为 public class App extends MultiDexApplication implements HasActivityInjector, HasFragmentInjector, HasSupportFragmentInjector,并随后提供适当的注入器。请参考这篇简单易懂的文章,其中详细说明了该主题。此外,还介绍了如何使用“HasSupportFragmentInjector”接口。

感谢@Andrey的帮助。 我在执行时遇到了以下错误== “java.lang.NullPointerException: com.arteciate.arteciate.di.App.supportFragmentInjector()返回null”。 - Sajid Zeb
你是否已经实现了 public AndroidInjector<Fragment> supportFragmentInjector() 方法,以便它返回有效的注入器? - nyarian

2
在尝试在片段的onCreateView中添加相同的行时,应该在片段的onAttach中完成AndroidInjection.inject(this)操作。如果您正在使用支持库中的片段,则应该使用AndroidSupportInjection.inject(this)。请注意保留HTML标签。
override fun onAttach(context: Context?) {
    AndroidSupportInjection.inject(this)
    super.onAttach(context)
}

感谢您的时间,Blackbelt。我忘记在帖子中添加这个了。我也尝试过这个方法。 现在,在您的评论后,我在MyFragment的onAttach方法中添加了AndroidSupportInjection.inject(this)。但是出现了这个问题。 - Sajid Zeb
java.lang.IllegalArgumentException: 找不到com.arteciate.arteciate.fragments.MyFragment的注入器 位于dagger.android.support.AndroidSupportInjection.findHasFragmentInjector(AndroidSupportInjection.java:92) 在dagger.android.support.AndroidSupportInjection.inject(AndroidSupportInjection.java:57)中 在com.arteciate.arteciate.fragments.HomeArtworksFragment.onAttach(HomeArtworksFragment.java:252)中 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1363)中...... - Sajid Zeb
如果我没记错的话,HasFragmentInjector 应该在托管片段的 Activity 上声明。 - Blackbelt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接