1

Today i am facing a problem in android app while executing AsyncTask.exception log is -

   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)

i am not getting what the error is and why app force close. This situation not occur after first run but from second run of app cause exception.

Thanks in advance.

EDITED-

i am using following method in DashBoardActivity for register to 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();
    }
}
Ravi Bhandari
  • 4,682
  • 8
  • 40
  • 68

3 Answers3

3

There are three ways to solve it:

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);
Meng
  • 181
  • 2
  • 6
1

You're returning actual objects, but you have it declared to return Void in the parameters for the task. Either don't return objects or change the generic parameters to match what you are returning.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
1

You are using something like this -

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

So this is generic AsyncTask to call execute, that class would pass Void as a parameter and will never call .execute() on your asynctask, instead it will call mAsyncTask.execute(Void). That gives the error.

Check this example implementation for such problems -object-cannot-be-cast-to-void-in-asynctask.

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
  • i have added mAsyncTask.execute(Void) but warning is create local variable Void. – Ravi Bhandari Jun 10 '14 at 08:33
  • Check my updated answer. You can't add `mAsyncTask.execute(Void)`. That is why you are getting the error. See link for detail and possible solution. – sjain Jun 10 '14 at 08:35
  • i have change my async task by creating a class and then call execute works from me.thanks for your support. – Ravi Bhandari Jun 10 '14 at 09:23