Ravi Bhandari
Ravi Bhandari

Reputation: 4698

FATAL EXCEPTION: AsyncTask

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

Upvotes: 1

Views: 3422

Answers (3)

Meng
Meng

Reputation: 181

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

Upvotes: 3

sjain
sjain

Reputation: 23344

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.

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93561

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.

Upvotes: 1

Related Questions