简体   繁体   English

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

[英]Unable to inject dependency in fragment using dagger2 in Java Android

I am able to inject dependency in activities and its working fine. 我能够在活动及其工作中注入依赖性。 but calling the same dependency in fragment is not taking access. 但是在片段中调用相同的依赖项不会获得访问权限。

This is what i did. 这就是我所做的。

App.Java 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;
    }
}

This is AppComponent.Java 这是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();
    }
}

This is ActivityModule.Java class 这是ActivityModule.Java类

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

    @ContributesAndroidInjector
    abstract MyActivity contributeCampaignActivityInjector();
}

this is FragmentModule.java class 这是FragmentModule.java类

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

Now in my MyFragment class where i want to inject that dependency. 现在在MyFragment类中,我想在其中注入该依赖项。 on top i add these lines inside class 在顶部,我将这些行添加到类中

@Inject
ImageDownloaderApi imageDownloaderApi;

then to check in onCreateView function i did this to check if my dependency is working like this 然后检查onCreateView函数,我这样做是为了检查我的依赖项是否像这样工作

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

And it is returning me false everytime. 每次都使我感到虚假。

where as same code works in activites. 同样的代码在activites中起作用的地方。 one more thing i am doing in activities is adding this line below onCreate like this 我在活动中要做的另一件事是像这样在onCreate下方添加此行

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

when in trying to add the same line in onCreateView in fragment 在尝试在片段中的onCreateView中添加同一行时

AndroidInjection.inject(this);    

it states red line below this. 它在此下方显示红线。 and if i replace this with getActivity. 如果我用getActivity替换它。 it gave me runtime error. 它给了我运行时错误。

Please have a look and guide where i am wrong? 请看看并指导我错了吗?

Is the process i am doing for fragment is right or not?. 我为片段执行的过程是否正确? as it is clearly working in activities. 因为它显然在活动中起作用。

Thanks in advance for your time. 在此先感谢您的时间。

I assume you are using Fragment -s from support library. 我假设您正在使用支持库中的Fragment -s。 Try to change public class App extends MultiDexApplication implements HasActivityInjector, HasFragmentInjector to public class App extends MultiDexApplication implements HasActivityInjector, HasFragmentInjector, HasSupportFragmentInjector with subsequent providing of appropriate injector. 尝试更改public class App extends MultiDexApplication implements HasActivityInjector, HasFragmentInjectorpublic class App extends MultiDexApplication implements HasActivityInjector, HasFragmentInjector, HasSupportFragmentInjector ,随后提供适当的注射器。

This is an easy article that makes this topic clear. 这是一篇简单的文章 ,使这个主题变得清晰。 Also, HasSupportFragmentInjector interface usage is also described here. 另外,此处还描述了HasSupportFragmentInjector接口的用法。

when in trying to add the same line in onCreateView in fragment 在尝试在片段中的onCreateView中添加同一行时

AndroidInjection.inject(this); AndroidInjection.inject(本);

this should be done in onAttach of your fragment, and if you are using the fragment from the support library, as you should, the line would be AndroidSupportInjection.inject(this) 这应该在片段的onAttach中完成,如果您正在使用支持库中的片段(如您onAttach ,则该行应为AndroidSupportInjection.inject(this)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM