0

I'm using AsyncHTTPClient(loopj.com/android-async-http/) in my app to excess json api. It works fine except when the user presses back button before asynchttpclient completes its work. It crashes the app. Here is my onFinish() method code.

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

Here is stackTrace

 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)

I'm new to android and if there is something wrong that I might be doing, please point out. Please help, Thanks in advance!

morten.c
  • 3,414
  • 5
  • 40
  • 45
lightsaber
  • 1,481
  • 18
  • 37
  • Did you end up finding a solution to your problem? I am having the same one. – Karim Feb 01 '14 at 14:46
  • Not really, but tried a lot of things and it worked. Answer given by indigomonkey below helped. I overrided all versions of onFailure method, checked variables like adapter, listview and context for them being null. Are you using actionBarSherlock and fragments? if so, also check for context returned from getActivity() being null. – lightsaber Feb 02 '14 at 06:55

2 Answers2

1

It's difficult to tell what the particular error is in your case without access to the line numbers in your method onFinish().

However, the top line of your stack trace shows that the NullPointerException occurred at

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

This shows that you need to find line 423 in your code and this will narrow down which variable with a value of null you are attempting to access.

For example, let's say you discover that line 423 is the following line:

lv.setAdapter(adapter);

You can now be sure that lv is null and debug why that might be.

Ben Cox
  • 1,393
  • 10
  • 28
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.

Iñaqui
  • 251
  • 4
  • 9