0

我在我的应用程序中使用 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新手,如果我可能做错了什么,请指出。请帮助,在此先感谢!

4

2 回答 2

1

如果不访问您的方法中的行号,很难说出您的具体错误是什么onFinish()

但是,堆栈跟踪的第一行显示NullPointerException发生在

com.example.ListFragment$2.onFinish(ListFragment.java:423)

这表明您需要在代码中找到第 423 行,这将缩小您尝试访问的值为 null 的变量的范围。

例如,假设您发现第 423 行是以下行:

lv.setAdapter(adapter);

您现在可以确定它lv为 null 并调试可能的原因。

于 2013-10-03T11:03:29.253 回答
1

If there are pending requests when the application is closed as a result of a back press you are likely to get a NullPointerException. Usually in the callback handler in my case. I found that calling .cancelRequest on the AsyncHttpClient fixed this problem. In my case I call it in the onDestroy() of my Activity which gets called whenever the back press occurs and the application exits.

Context context = this; // "this" is the Activity used by the client
client.cancelRequests(context, true);

The Javadoc explains this in the method definition.

于 2014-01-17T07:52:02.897 回答