匕首2注射无效

编程入门 行业动态 更新时间:2024-10-19 10:20:33
本文介绍了匕首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注射无效

本文发布于:2023-10-31 04:28:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545003.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:匕首

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!