基本Android AsyncTask,执行doInBackground()时发生错误
[已解决]
我正在尝试使用 AsyncTask 创建一个基本线程,如 Android API 文档中所示。但是,当我尝试执行最简单的线程时遇到错误。
AsyncTask task = new AsyncTask<Void, Integer, Boolean>() {
@Override
protected Boolean doInBackground(Void... arg0) {
try {
Thread.sleep(6000);
System.out.println("DELAYED TEST TEST TEST");
}catch(Exception e) {
}
return true;
}
};
Logcat 条目:
E/AndroidRuntime(21215): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(21215): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(21215): at android.os.AsyncTask$3.done(AsyncTask.java:200)
E/AndroidRuntime(21215): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
E/AndroidRuntime(21215): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
E/AndroidRuntime(21215): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
E/AndroidRuntime(21215): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
E/AndroidRuntime(21215): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
E/AndroidRuntime(21215): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
E/AndroidRuntime(21215): at java.lang.Thread.run(Thread.java:1027)
E/AndroidRuntime(21215): Caused by: java.lang.ClassCastException: [Ljava.lang.Object;
E/AndroidRuntime(21215): at hlf.scenes.Intro$2.doInBackground(Intro.java:1)
E/AndroidRuntime(21215): at android.os.AsyncTask$2.call(AsyncTask.java:185)
E/AndroidRuntime(21215): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
E/AndroidRuntime(21215): ... 4 more
如果有人能帮助我解决这个问题,我将非常感激! 感谢
修复:
AsyncTask<Void, Integer, Boolean> task = new AsyncTask<Void, Integer, Boolean>()
[solved]
I'm trying to create a basic thread using AsyncTask as shown in the documentation for the Android API. However, I'm coming across an error when trying to execute the most simplest of threads.
AsyncTask task = new AsyncTask<Void, Integer, Boolean>() {
@Override
protected Boolean doInBackground(Void... arg0) {
try {
Thread.sleep(6000);
System.out.println("DELAYED TEST TEST TEST");
}catch(Exception e) {
}
return true;
}
};
Logcat entry:
E/AndroidRuntime(21215): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime(21215): java.lang.RuntimeException: An error occured while executing doInBackground()
E/AndroidRuntime(21215): at android.os.AsyncTask$3.done(AsyncTask.java:200)
E/AndroidRuntime(21215): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
E/AndroidRuntime(21215): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
E/AndroidRuntime(21215): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
E/AndroidRuntime(21215): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
E/AndroidRuntime(21215): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
E/AndroidRuntime(21215): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
E/AndroidRuntime(21215): at java.lang.Thread.run(Thread.java:1027)
E/AndroidRuntime(21215): Caused by: java.lang.ClassCastException: [Ljava.lang.Object;
E/AndroidRuntime(21215): at hlf.scenes.Intro$2.doInBackground(Intro.java:1)
E/AndroidRuntime(21215): at android.os.AsyncTask$2.call(AsyncTask.java:185)
E/AndroidRuntime(21215): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
E/AndroidRuntime(21215): ... 4 more
If anyone could help me solve this I'd be really appreciative!
Thanks
Fix:
AsyncTask<Void, Integer, Boolean> task = new AsyncTask<Void, Integer, Boolean>()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这篇帖子解释了原因。基本上,由于您将引用声明为没有任何类型的
AsyncTask
,因此它期望所有内容都是对象。给出声明类型:并且它应该知道如何处理它。
This post explains why. Basically, since you are declaring the reference as
AsyncTask
without any types, it is expecting Objects for everything. Give the declaration types:and it should know how to handle it.
我不认为您调用带有任何参数的执行方法,是吗?
I don't suppose you call the execute method with any argument, do you?