构造函数注入中的空指针异常Dagger2 Android

时间:2018-04-09 19:29:48

标签: android dagger

我试图在我的Interactor类中注入上下文,它给了我一个空指针异常。 我使用了MVP模式,我试图访问我的非活动类中的上下文。 我不确定这是否是最好的技术。

模块:

@Module
public class ContextModule {

private final Context context;

public ContextModule(Context context) {
    this.context = context;
}

@Singleton
@Provides
public Context getContext() {
    return this.context;
}

}

组件:

@Singleton
@Component(modules = {ContextModule.class})
public interface AppComponent {

void inject(MainActivity mainActivity);
}

应用

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();
}
}

演示

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);
}
}

交互器

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);
}
}

摇篮

implementation 'com.google.dagger:dagger-android:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'

1 个答案:

答案 0 :(得分:0)

您没有在演示者中注入交互者 - 因此它不会有上下文。

您可能需要重构Presenter以将Interactor作为依赖项 - 这也意味着您需要重新构建完成侦听器的设置方式。