onPostExecute()无法重新运行从活动调用的asyncTask

时间:2013-09-20 08:48:22

标签: android android-asynctask

第一次如果我运行我的应用程序,那么在doinBackground()完成后控件转到onPostExecute()。但是如果我在没有卸载的情况下在设备上运行应用程序,则永远不会执行onPostExecute()。

我有一个活动,我正在调用我的asynTask。

代码:

public class AddThing extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
try {
            Class.forName("android.os.AsyncTask");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
private View.OnClickListener onSave = new View.OnClickListener() {
startAddThingType2Task();
}
}
private class ThingCallback implements ActionCallback {

        Context cntxt;

        public ThingCallback(Context context) {
            // TODO Auto-generated constructor stub
           this.cntxt = context;    
        }

        public void onSuccess(ArrayList<?> objects) {

            setProgressBarIndeterminate(false);
            Intent intent = new Intent(getApplicationContext(),DocketDetail.class);

            startActivityForResult(intent, 1);
        }

        public void onFailure(Exception exception) {            
            Toast.makeText(cntxt, "Unable to add docket. Error is: "+ exception.getMessage(), Toast.LENGTH_LONG).show();
        }       
    }
private void startAddThingType2Task() {


            final AddThingType2 task = new AddThingType2(AddThing.this,new ThingCallback(this));

            runOnUiThread(new Runnable() {

                public void run() {
                    // TODO Auto-generated method stub
                    task.execute();

                }
            });
}

我的asyncTask类是:

public class AddThingType2Task2 extends AsyncTask<Void, Void, Boolean> {

    public ActionCallback callback = null;
    private Context context;
    private Exception ePriv = null;
    private Activity activity;

    public AddThingType2(Activity activity,ActionCallback callback, ) {
        this.callback = callback;
        this.context = context;
        this.activity = activity;

    }

    @Override
    protected void onPostExecute(Boolean result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        if (result == true) {
            callback.onSuccess(null);
        } else {
            callback.onFailure(ePriv);
        }               
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        // TODO Auto-generated method stub


        Boolean retval = false;



        try {

            retval = true;
            }

        } catch (Exception e) {
            Log.e("AddThingType2Task", e.getMessage());
            ePriv = e;
        }

        return retval;
    }

}

ActionCallback 界面有两种方法 - onSuccess()OnFailure()

修改 我在我的应用程序中也使用 commonsware wakeful intent service

它第一次像魅力一样工作,但重新运行onpostExecute()不起作用......

请帮助!!

此致

重新运行后,我将在 logcat

中获取此信息
09-20 17:37:28.515: W/MessageQueue(9311): Handler{4060df08} sending message to a Handler on a dead thread
09-20 17:37:28.515: W/MessageQueue(9311): java.lang.RuntimeException: Handler{4060df08} sending message to a Handler on a dead thread
09-20 17:37:28.515: W/MessageQueue(9311):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)
09-20 17:37:28.515: W/MessageQueue(9311):   at android.os.Handler.sendMessageAtTime(Handler.java:457)
09-20 17:37:28.515: W/MessageQueue(9311):   at android.os.Handler.sendMessageDelayed(Handler.java:430)
09-20 17:37:28.515: W/MessageQueue(9311):   at android.os.Handler.sendMessage(Handler.java:367)
09-20 17:37:28.515: W/MessageQueue(9311):   at android.location.LocationManager$ListenerTransport.onStatusChanged(LocationManager.java:206)
09-20 17:37:28.515: W/MessageQueue(9311):   at android.location.ILocationListener$Stub.onTransact(ILocationListener.java:75)
09-20 17:37:28.515: W/MessageQueue(9311):   at android.os.Binder.execTransact(Binder.java:320)
09-20 17:37:28.515: W/MessageQueue(9311):   at dalvik.system.NativeStart.run(Native Method)

3 个答案:

答案 0 :(得分:0)

public class AddThing extends Activity {
   private ThingCallback callback;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ThingCallback = new ThingCallback(this);
            requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
try {
            Class.forName("android.os.AsyncTask");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
private View.OnClickListener onSave = new View.OnClickListener() {
startAddThingType2Task();
}
}
private class ThingCallback implements ActionCallback {
        Context cntxt;

        public ThingCallback(Context context) {
           this.cntxt = context;    
        }

        public void onSuccess(ArrayList<?> objects) {
            setProgressBarIndeterminate(false);
            Intent intent = new Intent(getApplicationContext(),DocketDetail.class);
            startActivityForResult(intent, 1);
        }

        public void onFailure(Exception exception) {            
            Toast.makeText(cntxt, "Unable to add docket. Error is: "+ exception.getMessage(), Toast.LENGTH_LONG).show();
        }       
    }

private void startAddThingType2Task() {
   final AddThingType2 task = new AddThingType2(AddThing.this, callback);
   task.execute();
}

答案 1 :(得分:0)

您使用的是其他任何线程,还有AsyncTask?如果不是,则不需要使用runOnUiThread。删除它,不要使AsyncTask引用最终。 AsyncTask永远不能执行两次,这就是为什么每次再次使用它时都必须将它实例化为新的。 final关键字将阻止新的实例化并使引用不可变。

private void startAddThingType2Task() {
   AddThingType2 task = new AddThingType2(AddThing.this,new ThingCallback(this));
   task.execute();
}

如果需要runOnUiThread,请考虑将AsyncTask实例声明为字段。这样你就不必在runOnUiThread中使用它。

编辑:

你会有这样的事情:

public class AddThing extends Activity {
        // Your field
        AddThingType2 task;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); 
            try {
                Class.forName("android.os.AsyncTask");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        private View.OnClickListener onSave = new View.OnClickListener() {
            startAddThingType2Task();
        }

        private void startAddThingType2Task() {
            // no final needed and you can instantiate it multiple times
            task = new AddThingType2(AddThing.this, callback);
            task.execute();
        }
}

答案 2 :(得分:0)

将此类加载到应用程序的onCreate

public class MyApplication extends android.app.Application{

   public void onCreate(){
     try {
       Class.forName("android.os.AsyncTask");
     } catch (ClassNotFoundException e) {
       e.printStackTrace();
     }

   }
}