【发布时间】:2018-09-17 13:31:45
【问题描述】:
关于 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
【问题讨论】:
-
分享你的堆栈跟踪
-
分享你的代码,通过按钮点击启动异步任务。
-
您是否尝试过将 MyTask 类放在单独的文件中?
-
rya - 我有一段时间不在家,只能访问免费版本的 AIDE(有限且已使用的额外页面)。它在 MainActivity.java 中工作,只要将代码放在该主页上的按钮中,它就不起作用。
-
我想问题出在上下文上。请分享按钮点击代码。
标签: java android android-asynctask classcastexception