繁体   English   中英

Android Java 匕首注入问题

[英]Android Java Dagger Inject Problems

我的应用组件

 @Singleton
 @Component(modules = {SplashModule.class})
   public interface AppComponent  {
     void inject(SplashActivity splashActivity);//this is inject activity
    
 }

飞溅模块

@Module
public class SplashModule {

   @Provides
   @Singleton
   static SplashInteract provideSplashInteract(){
      return new SplashInteract();//instance interact
    };

   @Provides
   @Singleton
    SplashPresenter provideSplashPresenter(SplashInteract splashInteract){
      return new SplashPresenter(splashInteract);//instance SplashPresenter
     };
 }

飞溅演示者

 public class SplashPresenter implements ISplashContract.Presenter {

  ISplashContract.View mView;
 SplashInteract splashInteract;


public SplashPresenter(SplashInteract splashInteract) {
    this.splashInteract =splashInteract;
}

public void bindView(ISplashContract.View mView) {
    this.mView = mView;
}

   @Override
    public void attach() {

    this.mView.startAnimation();//start splash animation
  }

  @Override
   public void start(Activity activity) {
    this.splashInteract.SplashScreenAnimation(activity);// add interact methods
    }
 }

飞溅活动

 public class SplashActivity extends AppCompatActivity implements ISplashContract.View{

 @Inject SplashPresenter splashPresenter;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    getAppComponent().inject(this);//call BaseApp DaggerAppComponent 
    splashPresenter.attach();
    splashPresenter.bindView(this);
  }


    @Override
    public void startAnimation() {
    this.splashPresenter.start(this);
   }

 }

基础应用

 public class BaseApp  extends Application {

 private  static AppComponent appComponent;

  @Override
  public void onCreate() {
    super.onCreate();
    setUp();
  }

   private void setUp() {
    appComponent = DaggerAppComponent.builder().build();//Call Component in BaseApp
   }

   public static AppComponent getAppComponent() {
    return appComponent;
   }
}

嗨,大家好

我正在写一个项目,我想使用匕首,但我对此缺乏经验。 此代码给出 NullPointerException 错误。 我找不到我做错了什么。

我需要帮助,如果那些比匕首更了解的人指导我,我会很高兴

确保在AndroidManifest中声明BaseApp

<application
        android:name=".fullPathTo.BaseApp"
        ...

在您的情况下,dagger 将不知道如何提供 splashPresenter,要么进行构造函数注入,要么在模块中定义如何创建 SplashPresenter,然后从组件中删除 SpashPresenter。 模块方法应该是这样的,

@Module
public class SplashModule {

 ISplashContract.View  mView;

   public SplashModule(ISplashContract.View mView) {
    this.mView = mView;//add SplashModule view
  }

  @Provides
  @Singleton
  public ISplashContract.View provideSplashPresenter(){
    return mView;//set this view
  }

   @Provides
   static SplashInteract provideSplashInteract(){
    return new SplashInteract();//instance interact 
  };

   @Provides
   SplashPresenter provideSplashPresenter(ISplashContract.View mView, SplashInteract splashInteract){
    return new SplashPresenter(mView, splashInteract);//instance SplashPresenter 
  };

 }

并从 SplashPresenter 中删除注入注释,您还必须更改其构造函数的签名。 如果演示者不应该是 singleton,您可以选择从代码中删除 Singleton 注释。

根据评论更新


 public class SplashModule {

   @Provides
   static SplashInteract provideSplashInteract(){
    return new SplashInteract();//instance interact 
  };

   @Provides
   SplashPresenter provideSplashPresenter(SplashInteract splashInteract){
    return new SplashPresenter(splashInteract);//instance SplashPresenter 
  };

 }
 public class SplashPresenter implements ISplashContract.Presenter {

   ISplashContract.View mView;
   SplashInteract splashInteract;

  public SplashPresenter(SplashInteract splashInteract) {
    this.splashInteract = splashInteract;
  }

  public void bindView(ISplashContract.View mView) {
    this.mView = mView;
  }

   @Override
    public void attach() {

    this.mView.startAnimation();//start splash animation
  }

  @Override
   public void start(Activity activity) {
    this.splashInteract.SplashScreenAnimation(activity);// add interact methods
    }
 }

public class SplashActivity extends AppCompatActivity implements ISplashContract.View{

 @Inject SplashPresenter splashPresenter;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    getAppComponent().inject(this);//call BaseApp DaggerAppComponent 
    splashPresenter.bind("the view you want to bind")
    splashPresenter.attach();
  }


    @Override
    public void startAnimation() {
    this.splashPresenter.start(this);
   }

 }```

暂无
暂无

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

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