Skip to content

Strange ClassCastException with AsyncTask

March 17, 2011

I was getting a pretty strange ClassCastException when calling execute on an AsyncTask. The exception was supposedly occurring at line 1 of the source file but this is impossible as line 1 is the package statement. The exception was being raised when the background thread called the doInBackground method which I then redirected to another method called doInBackground. The result was java.lang.ClassCastException: [Ljava.lang.Object;. That funny shaped class is, I’m pretty sure, the Object[] class.

After some digging I realized that this exception was happening because I was calling execute on a non-typesafe reference to an AsyncTask ie the reference had no generic parameters. I don’t think this should raise an exception but it looks like the Dalvik VM doesn’t like it. Alas. If you run into the same problem just make sure you’re calling execute on a type-safe reference.

About these ads

From → android

One Comment
  1. grateful anonymous permalink

    Thanks a bunch! I just ran into this today…luckily google indexed this page, I realize you just wrote this today as well :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: