Zane Claes
Zane Claes

Reputation: 14954

Android AsyncTask throwing mysterious RuntimeException

On rare occasion, I'm getting this mysterious RuntimeException in one of my AsyncTasks. I have multiple places where I implement AsyncTasks in my app, but no matter how much I dig into these errors I can't seem to get any info about why this RuntimeException is being thrown or what thread it is happening in. Based upon the "completedAbruptly=true" I'm guessing that there's an uncaught exception in the async task, but I don't even know where to begin to try to figure out where... ideas? Thanks!

enter image description here

Upvotes: 1

Views: 976

Answers (2)

zapl
zapl

Reputation: 63955

completedAbruptly=true happens when the executed task throws an exception. And the task of an AsyncTask is executing the doInBackground method.

I suggest you wrap your doInBackground code into a try/catch block and let it print exceptions that occur.

protected Void doInBackground(Void... params) {
    try {
        // ----------
        // do stuff
        return null;
        // ----------
    } catch (Throwable t) {
        Log.e("AsyncTask", "OMGCrash", t);
        // maybe throw it again
        throw new RuntimeException(t);
    }
}

That should show you the reason all this happens.

Upvotes: 4

alexfernandez
alexfernandez

Reputation: 1988

According to the source code, completedAbruptly is a sign that the thread finished due to user interaction. I would guess that the exception happens when an activity is finished before the async task completes. It should be trivial to discard this possibility: just replace one of your AsyncTasks with an infinite loop and press "back" on the emulator.

Upvotes: 2

Related Questions