AaronDS
AaronDS

Reputation: 861

Basic Android AsyncTask, an error occurred while executing doInBackground()

[solved]

I'm trying to create a basic thread using AsyncTask as shown in the documentation for the Android API. However, I'm coming across an error when trying to execute the most simplest of threads.

    AsyncTask task = new AsyncTask<Void, Integer, Boolean>() {

    @Override
    protected Boolean doInBackground(Void... arg0) {
        try {
            Thread.sleep(6000);
            System.out.println("DELAYED TEST TEST TEST");
        }catch(Exception e) {

        }

        return true;
    }
    };

Logcat entry:

E/AndroidRuntime(21215): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(21215): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(21215):        at android.os.AsyncTask$3.done(AsyncTask.java:200)
E/AndroidRuntime(21215):        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
E/AndroidRuntime(21215):        at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
E/AndroidRuntime(21215):        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
E/AndroidRuntime(21215):        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
E/AndroidRuntime(21215):        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
E/AndroidRuntime(21215):        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
E/AndroidRuntime(21215):        at java.lang.Thread.run(Thread.java:1027)
E/AndroidRuntime(21215): Caused by: java.lang.ClassCastException: [Ljava.lang.Object;
E/AndroidRuntime(21215):        at hlf.scenes.Intro$2.doInBackground(Intro.java:1)
E/AndroidRuntime(21215):        at android.os.AsyncTask$2.call(AsyncTask.java:185)
E/AndroidRuntime(21215):        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
E/AndroidRuntime(21215):        ... 4 more

If anyone could help me solve this I'd be really appreciative! Thanks

Fix:

AsyncTask<Void, Integer, Boolean> task = new AsyncTask<Void, Integer, Boolean>() 

Upvotes: 2

Views: 7704

Answers (2)

toadzky
toadzky

Reputation: 3846

This post explains why. Basically, since you are declaring the reference as AsyncTask without any types, it is expecting Objects for everything. Give the declaration types:

AsyncTask<Void, Integer, Boolean> task = new ...

and it should know how to handle it.

Upvotes: 3

njzk2
njzk2

Reputation: 39406

I don't suppose you call the execute method with any argument, do you?

Upvotes: 0

Related Questions