关闭时使用AsyncTask的活动崩溃

时间:2012-07-21 20:01:33

标签: android android-asynctask

我正在尝试按照以下pattern来保护活动被销毁时的后台任务。由于我想在不同的活动中重用我的任务,因此我为每个任务实现了一个“TaskMethod”类。

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

因此,每次我想在活动中使用任务时,我都会声明它

公共类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();
}

}

然而,当我的活动关闭时,我得到以下异常:

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)

我做错了什么?谢谢!

2 个答案:

答案 0 :(得分:1)

似乎当您在cleanUp()上调用task_时它是null,因此您只需要在进行该调用之前对该变量进行空值检查。希望这会有所帮助。

答案 1 :(得分:1)

当您完成活动时,会调用onDestroy()并在其中调用您自己的清理函数。

此函数中的一个变量为null,因此返回NullPointerException,因此无法销毁该活动。

在清理函数中放置一个断点并检查那里使用的变量值。其中一个(很可能是mAct_)为空。