致命异常:AsyncTask

时间:2014-06-10 08:20:08

标签: android android-asynctask threadpoolexecutor

今天我在Android应用程序中遇到问题,而执行AsyncTask.exception日志是 -

   FATAL EXCEPTION: AsyncTask #1
   java.lang.RuntimeException: An error occured while executing doInBackground()
   at android.os.AsyncTask$3.done(AsyncTask.java:299)
   at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
   at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
   at java.util.concurrent.FutureTask.run(FutureTask.java:239)
   at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
   at java.lang.Thread.run(Thread.java:838)
   Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to          java.lang.Void[]
   at com.android.demo.DashBoardActivity$3.doInBackground(DashBoardActivity.java:1)
   at android.os.AsyncTask$2.call(AsyncTask.java:287)
   at java.util.concurrent.FutureTask.run(FutureTask.java:234)

我没有得到错误是什么以及为什么app force关闭。 第一次运行后但第二次运行应用程序导致异常时不会发生这种情况。

提前致谢。

编辑 -

我在DashBoardActivity中使用以下方法注册GCM -

public void registerToGCM(){
    try{
          GCMRegistrar.checkDevice(this);
          GCMRegistrar.checkManifest(this);
          registerReceiver(mHandleMessageReceiver, new IntentFilter(
                    AppConstents.DISPLAY_MESSAGE_ACTION));

            final String regId = GCMRegistrar.getRegistrationId(this);
            if (regId.equals("")) {
                GCMRegistrar.register(this, AppConstents.SENDER_ID);
            } else {
                if (GCMRegistrar.isRegisteredOnServer(this)) {
                    Toast.makeText(getApplicationContext(), "Already registered with GCM", Toast.LENGTH_LONG).show();
                } else {
                    final Context context = this;
                    final AsyncTask mRegisterTask = new AsyncTask<Void, Void, Void>() {

                        @Override
                        protected Void doInBackground(Void... params) {
                            GCMServerUtilities.register(context,regId);
                            return null;
                        }

                        @Override
                        protected void onPostExecute(Void result) {
                          //  mRegisterTask = null;
                        }

                    };
                    mRegisterTask.execute(null, null, null);
                }
            }
    }
    catch(Exception e){
        e.printStackTrace();
    }
}

3 个答案:

答案 0 :(得分:3)

有三种方法可以解决它:

1:

final AsyncTask mRegisterTask = new AsyncTask<Object, Void, Void>() {.....

protected Void doInBackground(Object... params) {...

mRegisterTask.execute();

2:

final AsyncTask<Void, Void, Void> mRegisterTask = new AsyncTask<Void, Void, Void>() {.....

mRegisterTask.execute();

3:

final AsyncTask mRegisterTask = new AsyncTask<Void, Void, Void>() {.....

Void[] param = null;

mRegisterTask.execute(param);

答案 1 :(得分:1)

您正在返回实际对象,但您已将其声明为在任务的参数中返回Void。要么不返回对象,要么更改通用参数以匹配您返回的对象。

答案 2 :(得分:1)

你正在使用这样的东西 -

AsyncTask mAsyncTask = new yourAsynkTask(callback);
....
mAsyncTask.execute();

所以这是通用的AsyncTask来调用execute,该类会将Void作为参数传递,并且永远不会在你的asynctask上调用.execute(),而是调用mAsyncTask.execute(Void).这会产生错误。

检查此示例实现是否存在此类问题 - object-cannot-be-cast-to-void-in-asynctask