java–Dagger 2没有初始化变量

java–Dagger 2没有初始化变量,第1张

概述现在我正在尝试在我的项目中实施匕首,我有一个问题.在应用程序组件中初始化变量未在我的活动中初始化.这是我的代码.应用:publicclassMyApplicationextendsApplication{privateApplicationComponentapplicationComponent;@InjectpublicContextcontext;

现在我正在尝试在我的项目中实施匕首,我有一个问题.在应用程序组件中初始化的变量未在我的活动中初始化.这是我的代码.
应用:

public class MyApplication extends Application {    private ApplicationComponent applicationComponent;    @Inject    public Context context;    @OverrIDe    public voID onCreate() {        super.onCreate();        applicationComponent = DaggerApplicationComponent.builder().applicationModule(new ApplicationModule(this)).build();        applicationComponent.inject(this);    }    public ApplicationComponent getApplicationComponent() {        return applicationComponent;    }}

这里变量context!= null

活动:

public class MainActivity extends AppCompatActivity {    private ActivityComponent activityComponent;    @Inject    Context context;    @OverrIDe    protected voID onStart() {        super.onStart();        activityComponent = DaggerActivityComponent.builder()                .activityModule(new ActivityModule(this))                .applicationComponent(((MyApplication) getApplication()).getApplicationComponent())                .build();        activityComponent.inject(this);    }}

这里context == null.

匕首:

@Component(modules = {ApplicationModule.class})public interface ApplicationComponent {    voID inject(MyApplication application);    Context getApplicationContext();}@Component(modules = {ActivityModule.class}, dependencIEs = {ApplicationComponent.class})public interface ActivityComponent {    voID inject(MainActivity mainActivity);}@Modulepublic class ApplicationModule {    private Application application;    public ApplicationModule(MyApplication application) {        this.application = application;    }    @ProvIDes    Context provIDeApplicationContext() {        return application.getApplicationContext();    }}@Modulepublic class ActivityModule {    Activity activity;    public ActivityModule(MainActivity activity) {        this.activity = activity;    }}

因此,dagger仅在应用程序类中提供“Context”,但不在MainActivity类中提供它.我做错了什么?可能是我要改变一些东西.

谢谢!

解决方法:

设置如下:

public class MyApplication extends Application {  private ApplicationComponent applicationComponent;  @OverrIDe  public voID onCreate() {    super.onCreate();    applicationComponent = DaggerApplicationComponent.builder()            .applicationModule(new ApplicationModule(this))            .build();  }  public ApplicationComponent getApplicationComponent() {    return applicationComponent;  }}

和你的活动:

public class MainActivity extends AppCompatActivity {  @Inject  Context context;  @OverrIDe  protected voID onStart() {    super.onStart();    ((MyApplication) getApplication()).getApplicationComponent().inject(this);  }}
总结

以上是内存溢出为你收集整理的java – Dagger 2没有初始化变量全部内容,希望文章能够帮你解决java – Dagger 2没有初始化变量所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1119837.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存