Android Java Dagger 注入问题
Android Java Dagger Inject Problems
我的 AppComponent
@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
};
}
Splash 演示者
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
}
}
飞溅Activity
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;
}
}
大家好
我正在写一个项目,想用dagger,但是我对此没有经验。此代码给出 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 中删除注入注释,您还必须更改其构造函数的签名。如果演示者不应该是单身人士,您可以选择从代码中删除单身人士注释。
根据评论更新
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);
}
}```
我的 AppComponent
@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
};
}
Splash 演示者
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
}
}
飞溅Activity
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;
}
}
大家好
我正在写一个项目,想用dagger,但是我对此没有经验。此代码给出 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 中删除注入注释,您还必须更改其构造函数的签名。如果演示者不应该是单身人士,您可以选择从代码中删除单身人士注释。
根据评论更新
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);
}
}```