Search code examples
javaandroidandroid-asynctaskclasscastexception

AsyncTask isn't acting correctly


I'm having a bit of a breakdown regarding AsyncTask running one moment but not the next. I have simplified the code as much as possible. The try/catch code below launches my async task when called from MainActivity on app start-up. However, when I put this exact same code into a button which is within the same class file, a classCastException error crashes the app. Is there something I am not understanding about Async? Thanks all.

CODE THAT CALLS ASYNC:

findViewById(R.id.errorbtn).setOnClickListener( new View.OnClickListener() {
    @Override
    public void onClick(View v) {
try {
    new MainActivity.MyTask().execute(this);
}
catch (Exception e) {
    e.printStackTrace();
}
}});

ASYNC TASK:

private class MyTask extends AsyncTask<Object, Void, String> {

MainActivity activity;

@Override
protected String doInBackground(Object... params) {
    activity = (MainActivity)params[0];
    try {
        StringBuilder sb = new StringBuilder();
        URL url = new URL("https://www.example.com");
        BufferedReader in;
        in = new BufferedReader(new InputStreamReader(url.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null)
        sb.append(inputLine);
        in.close();

        html = sb.toString();
    }
    catch (Exception e) {
        e.printStackTrace();
    }

    // SOME WORK IS DONE USING HTML VARIABLE & URL DATA...
}

    @Override
    protected void onPostExecute(String str) {

        // RESULT IS LOADED INTO LIST VIEW...
    }
}

I THINK THIS IS WHAT YOURE AFTER:

09-17 13:09:12.612 17883 17883 I   art com.mycompany.rns                        Late-enabling -Xcheck:jni
09-17 13:09:12.612 17883 17883 I   art com.mycompany.rns                        VMHOOK: rlim_cur : 0 pid:17883
09-17 13:09:12.672 17883 17903 I   System com.mycompany.rns                     exec(logcat -v threadtime @ adrt.ADRTLogCatReader.run:42)
09-17 13:09:12.752 17883 17883 D   Atlas com.mycompany.rns                      Validating map...
09-17 13:09:12.782 17883 17907 I   Adreno-EGL com.mycompany.rns                 <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.BF.1.1_RB1.05.00.00.002.030_msm8974_refs/tags/AU_LINUX_ANDROID_LA.BF.1.1_RB1.05.00.00.002.030__release_AU ()
09-17 13:09:12.782 17883 17907 I   Adreno-EGL com.mycompany.rns                 OpenGL ES Shader Compiler Version: E031.25.03.00
09-17 13:09:12.782 17883 17907 I   Adreno-EGL com.mycompany.rns                 Build Date: 12/11/14 Thu
09-17 13:09:12.782 17883 17907 I   Adreno-EGL com.mycompany.rns                 Local Branch:
09-17 13:09:12.782 17883 17907 I   Adreno-EGL com.mycompany.rns                 Remote Branch: refs/tags/AU_LINUX_ANDROID_LA.BF.1.1_RB1.05.00.00.002.030
09-17 13:09:12.782 17883 17907 I   Adreno-EGL com.mycompany.rns                 Local Patches: NONE
09-17 13:09:12.782 17883 17907 I   Adreno-EGL com.mycompany.rns                 Reconstruct Branch: NOTHING
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             FATAL EXCEPTION: AsyncTask #1
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             Process: com.mycompany.rns, PID: 17883
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             java.lang.RuntimeException: An error occured while executing doInBackground()
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             at android.os.AsyncTask$3.done(AsyncTask.java:300)
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             at java.util.concurrent.FutureTask.run(FutureTask.java:242)
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             at java.lang.Thread.run(Thread.java:818)
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             Caused by: java.lang.ClassCastException: com.mycompany.rns.MainActivity$100000003 cannot be cast to com.mycompany.rns.MainActivity
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             at com.mycompany.rns.MainActivity$MyTask.doInBackground(MainActivity.java:624)
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             at com.mycompany.rns.MainActivity$MyTask.doInBackground(MainActivity.java)
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-17 13:09:48.952 17883 18854 E   AndroidRuntime com.mycompany.rns             ... 4 more
09-17 13:09:50.632 17883 18854 D   Process com.mycompany.rns                    killProcess, pid=17883

Solution

  • The problem is that you're creating the AsyncTask with a reference to a View.OnClickListener().

    Modify the creation line to this:

    new MainActivity.MyTask().execute(MainActivity.this);
    

    and it should work.

    The reason is simple: that line is inside the OnClickListener, and therefore 'this' refers to the Listener, not the activity. You could be more expressive by qualifying the AsyncTask, like so:

    AsyncTask<MainActivity, Void, String>