我对 AsyncTask 运行这一刻但没有下一刻的情况有所了解。我尽可能地简化了代码。下面的 try/catch 代码在应用程序启动时从 MainActivity 调用时启动我的异步任务。但是,当我将完全相同的代码放入同一类文件中的按钮中时,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
问题是你正在创建引用 View.OnClickListener(( 的 AsyncTask。
将创建行修改为:
new MainActivity.MyTask().execute(MainActivity.this);
它应该有效。
原因很简单:该行位于 OnClickListener 内部,因此"this"指的是侦听器,而不是活动。通过限定 AsyncTask,您可以更具表现力,如下所示:
AsyncTask<MainActivity, Void, String>
你需要像这样构建你的代码(你的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...
}