Michael Eilers Smith
Michael Eilers Smith

Reputation: 8618

Activity using AsyncTask crashes when closing

I am trying to follow the following pattern to secure my background tasks when an activity is destroyed. Since I wanted to reuse my tasks in different activities, I implemented a "TaskMethod" class for each task.

public class TaskMethod
{
   private SomeActivity mAct_ = null;
   private MyTask mTask_ = null;

    public TaskMethod(SomeActivity act)
    {
        mAct_ = act;
    }

    public void execute()
    {
           mTask_ = (MyTask) mAct_.getLastNonConfigurationInstance();

           if (mTask_ != null)
           {
                mTask_.activity_ = mAct_;

                if (mTask_.isFinished_)
                   mTask_.updateUI();
           }
           else
           {
                mTask_ = new MyTask();
                mTask_.activity_ = mAct_;
                mTask_.execute();
           }
    }

    public void cleanUp()
    {
           myTask_.activity_ = null;

           if (mAct_.isFinishing())
            myTask_.cancel(false);
    }

    static class MyTask extends AsyncTask<Void, Void, String>
    {

       @Override
        protected String doInBackground(Void... params)
        {
            ...
        }

    @Override
    protected void onPostExecute(String result) 
        {
            ...
            isFinished_ = true;
            updateUI();
        }

        public void updateUI()
        {
            if (activity_ != null)
            {
                ...
            }            
        }

        SomeActivity activity_ = null;
        boolean isFinished_  = false;
}

So, each time I want to use a task in an activity, I declare it

public class SomeActivity {

private TaskMethod task_;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
{
        super.onCreate(savedInstanceState);

        task_ = new TaskMethod(this);
        task_.execute();
    }

@Override
public void onDestroy()
{
    super.onDestroy();

    task_.cleanUp();
}

}

However, when my activity closes, I get the following exception:

07-21 19:37:15.195: E/AndroidRuntime(25724): FATAL EXCEPTION: main
07-21 19:37:15.195: E/AndroidRuntime(25724): java.lang.RuntimeException: Unable to destroy activity 
{com.signals.signals/com.signals.signals.activity.PreferencesActivity}: java.lang.NullPointerException
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3108)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3126)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.ActivityThread.access$1200(ActivityThread.java:122)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1179)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.os.Looper.loop(Looper.java:137)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.ActivityThread.main(ActivityThread.java:4340)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at java.lang.reflect.Method.invokeNative(Native Method)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at java.lang.reflect.Method.invoke(Method.java:511)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at dalvik.system.NativeStart.main(Native Method)
07-21 19:37:15.195: E/AndroidRuntime(25724): Caused by: java.lang.NullPointerException
07-21 19:37:15.195: E/AndroidRuntime(25724):    at com.signals.signals.activity.tasks.CitiesTaskMethod.cleanUp(CitiesTaskMethod.java:92)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at com.signals.signals.activity.PreferencesActivity.onDestroy(PreferencesActivity.java:117)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.Activity.performDestroy(Activity.java:4629)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1079)
07-21 19:37:15.195: E/AndroidRuntime(25724):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3095)

What am I doing wrong?? Thanks!

Upvotes: 0

Views: 463

Answers (2)

Erol
Erol

Reputation: 6526

When you are finishing your activity, onDestroy() is called and inside that, you are calling your own cleanup function.

One of the variables in this function is null so it is returning NullPointerException and hence unable to destroy the activity.

Put a breakpoint inside your cleanup function and check values of variables used there. One of them (most probably mAct_) is null.

Upvotes: 1

Egor
Egor

Reputation: 40228

It seems like when you're calling cleanUp() on the task_ it is null, so you just need to null-check the variable before making that call. Hope this helps.

Upvotes: 1

Related Questions