AsyncTask操作不正确

时间:2018-09-17 13:31:45

标签: java android android-asynctask classcastexception

关于AsyncTask一次运行而不是下一分钟的运行,我有一些故障。我已尽可能简化了代码。在应用启动时从MainActivity调用下面的try / catch代码会启动我的异步任务。但是,当我将完全相同的代码放入相同类文件中的按钮时,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$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

2 个答案:

答案 0 :(得分:2)

问题是您正在创建对View.OnClickListener()的引用的AsyncTask。

将创建行修改为此:

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

它应该可以工作。

原因很简单:该行位于OnClickListener内,因此“ this”是指侦听器,而不是活动。通过限定AsyncTask可以使您更具表现力,如下所示:

AsyncTask<MainActivity, Void, String>

答案 1 :(得分:0)

您需要像这样构造代码(您的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...
    }