AsyncTask 未正确运行

AsyncTask isn't acting correctly

我对 AsyncTask 运行 这一刻有点崩溃,但下一秒就不会了。我已经尽可能地简化了代码。下面的 try/catch 代码在应用程序启动时从 MainActivity 调用时启动我的异步任务。但是,当我将完全相同的代码放入同一 class 文件中的按钮时,classCastException 错误会使应用程序崩溃。我对异步有什么不了解的地方吗?谢谢大家

调用异步的代码:

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

异步任务:

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...
    }
}

我想这就是你想要的:

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.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.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.MainActivity0000003 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.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

您需要像这样构建您的代码(您的 onPostExecute 方法在错误的位置):

@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...
    }

问题是您在创建 AsyncTask 时引用了 View.OnClickListener()。

将创建行修改为:

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

它应该可以工作。

原因很简单:该行位于 OnClickListener 内部,因此 'this' 指的是监听器,而不是 activity。您可以通过限定 AsyncTask 来更具表现力,如下所示:

AsyncTask<MainActivity, Void, String>