Search code examples
androidlistviewondestroy

Android onDestroy Error


In my app there is one activity I have where I call onDestroy and inside it I have the following:

@Override
public void onDestroy()
{
    productAdapter.imageLoader.stopThread();

    lvProducts.setAdapter(null);
    super.onDestroy();
}

productAdapter is my ListView adapter and I am using the fedorvlasov lazy load functionality.

The issue is, when I leave the app for a LONG time and come back, I try to access this activity from the main activity and I get the following:

03-22 09:15:17.684: ERROR/AndroidRuntime(28442): FATAL EXCEPTION: main
03-22 09:15:17.684: ERROR/AndroidRuntime(28442): java.lang.RuntimeException: Unable to destroy activity {com.MyApp/com.MyApp.Products}: java.lang.NullPointerException
03-22 09:15:17.684: ERROR/AndroidRuntime(28442):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3081)
03-22 09:15:17.684: ERROR/AndroidRuntime(28442):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3146)
03-22 09:15:17.684: ERROR/AndroidRuntime(28442):     at android.app.ActivityThread.access$2100(ActivityThread.java:132)
03-22 09:15:17.684: ERROR/AndroidRuntime(28442):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1071)
03-22 09:15:17.684: ERROR/AndroidRuntime(28442):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-22 09:15:17.684: ERROR/AndroidRuntime(28442):     at android.os.Looper.loop(Looper.java:150)
03-22 09:15:17.684: ERROR/AndroidRuntime(28442):     at android.app.ActivityThread.main(ActivityThread.java:4263)
03-22 09:15:17.684: ERROR/AndroidRuntime(28442):     at java.lang.reflect.Method.invokeNative(Native Method)
03-22 09:15:17.684: ERROR/AndroidRuntime(28442):     at java.lang.reflect.Method.invoke(Method.java:507)
03-22 09:15:17.684: ERROR/AndroidRuntime(28442):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-22 09:15:17.684: ERROR/AndroidRuntime(28442):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-22 09:15:17.684: ERROR/AndroidRuntime(28442):     at dalvik.system.NativeStart.main(Native Method)
03-22 09:15:17.684: ERROR/AndroidRuntime(28442): Caused by: java.lang.NullPointerException
03-22 09:15:17.684: ERROR/AndroidRuntime(28442):     at com.MyApp.Productss.onDestroy(DailyDeals.java:678)
03-22 09:15:17.684: ERROR/AndroidRuntime(28442):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3063)

The thing is, I dont know how to reproduce this because it only happens if I leave the app for a long time and come back. Its almost like there is an Android timeout that says after this many hours, cause app to start over.


Solution

  • The system will kill your application whenever it needs the resources to use for something else. This is why when you come back after a long time it is no longer running.

    why do you have this line?

    lvProducts.setAdapter(null);
    

    I think that is what is giving you a null pointer. It isn't liking that you are trying to set it with a null adapter. Try removing this line

    Also just to be sure, which line is 678?