java.lang.NullPointerException: Attempt to invoke interface method 'void mao.com.mao_wanandroid_client.presenter.test.TestsContract$TestView.showTestSuccess()' on a null object reference
at mao.com.mao_wanandroid_client.presenter.test.TestPresenter$1.onSuccess(TestPresenter.java:60)
at mao.com.mao_wanandroid_client.presenter.test.TestPresenter$1.onSuccess(TestPresenter.java:56)
at mao.com.mao_wanandroid_client.model.http.control.BaseObserver.onNext(BaseObserver.java:30)
at mao.com.mao_wanandroid_client.model.http.control.BaseObserver.onNext(BaseObserver.java:14)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:200)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:105)
java.lang.NullPointerException: Attempt to invoke interface method 'void mao.com.mao_wanandroid_client.presenter.test.TestsContract$TestView.showTestSuccess()' on a null object reference at mao.com.mao_wanandroid_client.presenter.test.TestPresenter$1.onSuccess(TestPresenter.java:60) at mao.com.mao_wanandroid_client.presenter.test.TestPresenter$1.onSuccess(TestPresenter.java:56) at mao.com.mao_wanandroid_client.model.http.control.BaseObserver.onNext(BaseObserver.java:30) at mao.com.mao_wanandroid_client.model.http.control.BaseObserver.onNext(BaseObserver.java:14) at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:200) at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252) at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109) at android.os.Handler.handleCallback(Handler.java:907) at android.os.Handler.dispatchMessage(Handler.java:105)
TestContract代码
/**
@Description */ public interface TestsContract { interface TestView extends BaseView { void showTestSuccess();
}
interface TestActivityPresenter extends AbstractBasePresenter {
void getTest(Context context);
}
}
TestPresenter代码 /**
@Description */ public class TestPresenter extends RxBasePresenter implements TestsContract.TestActivityPresenter {
private DataClient mDataClient;
@Inject public TestPresenter(DataClient dataClient) { super(dataClient); this.mDataClient = dataClient; }
//绑定View @Override public void attachView(TestsContract.TestView view) { super.attachView(view); }
@Override public void getTest(Context context) { // Observable<ResponseBody<List>> homePageBannerObservable = mDataClient.GetHomePageBannerData();
// //获取 首页Banner 数据
// homePageBannerObservable.compose(RxSchedulers.observableIO2Main())
// .subscribe(new ProgressObserver<List>(context, context.getString(R.string.registered)) {
// @Override
// public void onSuccess(List result) {
// mView.showTestSuccess(result.get(0) + "");
// }
//
// @Override
// public void onFailure(Throwable e, String errorMsg) {
// mView.showTestFail(errorMsg);
// }
// });
Observable<ResponseBody> responseBodyObservable = mDataClient.postLoginData("zes", "123456789");
responseBodyObservable.compose(RxSchedulers.observableIO2Main(context))
.subscribe(new ProgressObserver(context, context.getString(R.string.landing)) {
@Override
public void onSuccess(LoginData result) {
Log.i("----------", Thread.currentThread().getName());
mView.showTestSuccess();
}
} }
主TestActivity代码: /**
@Description */ public class TestActivity extends BaseActivity implements TestsContract.TestView {
@BindView(R.id.toolbar)
Toolbar mToolbar;
@BindView(R.id.btn_click)
Button btnClick;
@BindView(R.id.tv_test)
TextView mTv;
@Override protected int getLayout() { return R.layout.activity_test; }
@Override protected void initToolbar() { mToolbar.setTitle(getString(R.string.signin)); mToolbar.setTitleTextColor(Color.WHITE); setSupportActionBar(mToolbar); mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_24dp); StatusBarUtil.setColorNoTranslucentLightMode(this, ContextCompat.getColor(this, R.color.colorPrimary)); mToolbar.setNavigationOnClickListener(v -> finish()); }
@Override protected void onViewCreated() { //初始化的View位置
}
@Override protected void initEventAndData() { mTv.setText("数据请求结果显示"); }
@OnClick({R.id.btn_click}) public void OnClickView(View view) { switch (view.getId()) { case R.id.btn_click: mPresenter.getTest(this); break; } }
@Override public void showTestSuccess() { Log.i("----------", Thread.currentThread().getName()); finish(); }
@Override public void showTestFail(String errorMsg) { mTv.setText(errorMsg); } }
每次请求成功后,调用 mView.showTestSuccess(),而主TestActivity中的 @Override public void showTestSuccess() { Log.i("----------", Thread.currentThread().getName()); finish(); } 报错了,我不知道咋回事,能否指点迷津。。。。