匕首2注射无效 [英] Dagger 2 injection not working

查看:109
本文介绍了匕首2注射无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供Gson,Retrofit和OkHttpClient单例的模块

The module that proides singletons of Gson, Retrofit and OkHttpClient

@Module
public class MyModule {

    @Provides
    @Singleton
    Gson provideGson() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        return gsonBuilder.create();
    }

    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient() {
        OkHttpClient client = new OkHttpClient();
        return client;
    }

    @Provides
    @Singleton
    Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .baseUrl(BuildConfig.SERVER_BASE_URL)
                .client(okHttpClient)
                .build();
        return retrofit;
    }
}

允许将单例插入活动和片段的组件

The component that allows injecting the Singletons into activity and fragments

@Singleton
@Component(modules={MyModule.class})
public interface MyComponent {

    void inject(Activity activity);
    void inject(Fragment fragment);
    void inject(Application application);
}

构建组件的主应用程序类

The main application class that builds the component

public class MyApp extends Application{


    private MyComponent component;

    @Inject
    Retrofit retrofit;

    @Override
    public void onCreate() {
        super.onCreate();
        component= DaggerMyComponent.builder()
                .myModule(new MyModule()).build();
        getComponent().inject(this); // inject retrofit here
    }

    public MyComponent getComponent() {
        return component;
    }
}

这是我要注入改造的片段

This is the Fragment where I am trying to inject Retrofit.

public class MyFragment extends Fragment {

    @Inject
    Retrofit retrofit;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        ((MyApp)getActivity().getApplication()).getComponent().inject(this);
      ....

    }
}

在MyApp和MyFragment中,改装实例均为null。

In both MyApp and MyFragment the retrofit instance is null.

推荐答案

您可以在同一组件中注入Activity,Fragment和Application。需要为每个Activity,Fragment和Component创建单独的组件,例如:

You can inject Activity,Fragment and Application in Same Component.You need to create Separate component for each Activity ,Fragment and Component Like this:

Activiy

在所有活动中使用此组件:

Use this component in all your activity:

@Singleton
@Component(modules={MyModule.class})
public interface MyActivityComponent {
    void inject(Activity activity);
    void inject(AnotherActivity activity);
}

像这样注入活动:

component= DaggerMyActivityComponent.builder()
                .myModule(new MyModule()).build();
        getComponent().inject(this)

片段

在所有片段中使用此组件:

Use this component in all your fragment:

    @Singleton
    @Component(modules={MyModule.class})
    public interface MyFragmentComponent {

        void inject(Fragment fragment);
        void inject(AnotherFragmen fragment);
    }

像这样插入片段:

component= DaggerMyFragmentComponent.builder()
                .myModule(new MyModule()).build();
        getComponent().inject(this)

应用

在应用程序中使用此组件:

Use this component in your Application:

    @Singleton
    @Component(modules={MyModule.class})
    public interface MyAppComponent {
        void inject(Application application);
    }

像这样注入应用程序:

component= DaggerMyAppComponent.builder()
                .myModule(new MyModule()).build();
        getComponent().inject(this)

这篇关于匕首2注射无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆