首先感谢您阅读我的问题。
现在我正在使用OKHTTP实现我的Android应用程序的Web服务。如果我使用快速互联网,应用程序运行速度很快。在慢速互联网上我慢慢得到数据也没关系。但问题是,如果我将一个活动切换到另一个活动而不等待响应数据调用的数据。那时它无法找到当前控件并抛出异常NullPointer。在这种情况下如何处理NullPointer错误。
它在当前活动中找到以前的活动ui控件来设置数据。
FATAL EXCEPTION: OkHttp Dispatcher
Process: in.cess, PID: 16535
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.runOnUiThread(java.lang.Runnable)' on a null object reference
at in.cess.fragment.WorkRequestCompletedFragment.onResponse(WorkRequestCompletedFragment.java:221)
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:177)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java:33)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
响应的活动代码。
this.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mAdapter = new CallAdapter(getActivity(),movieList,
new CallAdapter.OnItemClickListener() {
@Override
public void onFeedClicked(CallDeatails jobDetails) {
Intent i = new Intent(getActivity(), SalesCallDetailsActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(Const.INTENT_DATA,jobDetails);
i.putExtra(Const.INTENT_TODAY,"true");
getActivity().startActivity(i);
}
});
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
txt_total_call.setText("Total Call : "+String.valueOf(movieList.size()));
}
});