[英]Null Pointer Exception in Constructor Injection Dagger2 Android
I am trying to inject context in my Interactor class which is giving me a null pointer exception. 我试图在我的Interactor类中注入上下文,这给了我一个空指针异常。 I have used the MVP pattern and I am trying to get access to the context in my non-activity class. 我已经使用了MVP模式,并且试图在非活动类中访问上下文。 I am not really sure if this is the best technique used. 我不确定这是否是最好的技术。
Module: 模块:
@Module
public class ContextModule {
private final Context context;
public ContextModule(Context context) {
this.context = context;
}
@Singleton
@Provides
public Context getContext() {
return this.context;
}
} }
Component: 零件:
@Singleton
@Component(modules = {ContextModule.class})
public interface AppComponent {
void inject(MainActivity mainActivity);
}
App 应用程式
public class App extends Application {
private AppComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
appComponent = DaggerAppComponent.builder()
.contextModule(new ContextModule(this))
.build();
}
public AppComponent getAppComponent() {
return appComponent;
}
}
MainActivity 主要活动
public class MainActivity extends AppCompatActivity implements
TaskContract.IMainView {
@Inject
MainInteractor mainInteractor;
private MainPresnter mainPresnter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((App) getApplication()).getAppComponent().inject(this);
mainPresnter = new MainPresnter(this);
}
@Override
public void getRandomNumber(int rNum) {
Toast.makeText(this, "" + rNum, Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
super.onResume();
mainPresnter.fetchFromService();
}
}
Presenter 主持人
public class MainPresnter implements TaskContract.IMainPresenter,
TaskContract.OnTaskCompletionResult {
private TaskContract.IMainView mainView;
private MainInteractor mainInteractor;
public MainPresnter(TaskContract.IMainView mainView) {
this.mainView = mainView;
mainInteractor = new MainInteractor(this);
}
@Override
public void fetchFromService() {
mainInteractor.callService();
}
@Override
public void onSuccess(int rNum) {
mainView.getRandomNumber(rNum);
}
}
Interactor 互动者
public class MainInteractor implements TaskContract.IMainInteractor {
private static final int JOB_ID = 100 ;
private Context context;
@Inject
public MainInteractor(Context context) {
this.context = context;
}
public MainInteractor(TaskContract.OnTaskCompletionResult completionListener)
{
TaskService.setCompletionListener(completionListener);
}
@Override
public void callService() {
JobInfo jobInfo = new JobInfo.Builder(JOB_ID,
new ComponentName(context, TaskService.class))
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPeriodic(10000)
.build();
JobScheduler jobScheduler = (JobScheduler)
context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(jobInfo);
}
}
Gradle 摇篮
implementation 'com.google.dagger:dagger-android:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
You don't inject the Interactor within your Presenter - therefore it won't have a context. 您无需在Presenter中注入Interactor-因此它将没有上下文。
You could probably restructure your Presenter to require the Interactor as a dependency - this would also mean you'd need to restructure how the completion listener is set. 您可能需要重组Presenter以将Interactor作为依赖项-这也意味着您需要重组完成侦听器的设置方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.