我在我的应用程序中使用 AsyncHTTPClient(loopj.com/android-async-http/) 来增加 json api。它工作正常,除非用户在 asynchttpclient 完成其工作之前按下返回按钮。它使应用程序崩溃。这是我的onFinish()
方法代码。
@Override
public void onFinish(){
super.onFinish();
if(nodata == false){
getActivity().runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
adapter=new AlbumListAdapter(AlbumsListActivity.this, tracksList);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
setTitle(activity_title);
findViewById(R.id.loading_layout).setVisibility(View.GONE);
lv.setVisibility( View.VISIBLE );
}
});
}else{
runOnUiThread(new Runnable() {
public void run() {
adapter=new AlbumListAdapter(AlbumsListActivity.this, tracksList);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
findViewById(R.id.loading_layout).setVisibility(View.GONE);
lv.setEmptyView(findViewById(R.id.empty_list_item));
lv.setVisibility( View.VISIBLE );
}
});
}
}
});
这是堆栈跟踪
FATAL EXCEPTION: main
java.lang.NullPointerException
10-03 15:58:02.732: E/AndroidRuntime(19532): at com.example.ListFragment$2.onFinish(ListFragment.java:423)
10-03 15:58:02.732: E/AndroidRuntime(19532): at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:194)
10-03 15:58:02.732: E/AndroidRuntime(19532): at com.loopj.android.http.AsyncHttpResponseHandler$1.handleMessage(AsyncHttpResponseHandler.java:84)
10-03 15:58:02.732: E/AndroidRuntime(19532): at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 15:58:02.732: E/AndroidRuntime(19532): at android.os.Looper.loop(Looper.java:137)
10-03 15:58:02.732: E/AndroidRuntime(19532): at android.app.ActivityThread.main(ActivityThread.java:4448)
10-03 15:58:02.732: E/AndroidRuntime(19532): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 15:58:02.732: E/AndroidRuntime(19532): at java.lang.reflect.Method.invoke(Method.java:511)
10-03 15:58:02.732: E/AndroidRuntime(19532): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-03 15:58:02.732: E/AndroidRuntime(19532): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-03 15:58:02.732: E/AndroidRuntime(19532): at dalvik.system.NativeStart.main(Native Method)
我是android新手,如果我可能做错了什么,请指出。请帮助,在此先感谢!