基本Android AsyncTask,执行doInBackground()时发生错误

发布于 2024-12-09 08:20:08 字数 1993 浏览 0 评论 0原文

[已解决]

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红ご颜醉 2024-12-16 08:20:09

这篇帖子解释了原因。基本上,由于您将引用声明为没有任何类型的 AsyncTask ,因此它期望所有内容都是对象。给出声明类型:

AsyncTask<Void, Integer, Boolean> task = new ...

并且它应该知道如何处理它。

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:

AsyncTask<Void, Integer, Boolean> task = new ...

and it should know how to handle it.

雪落纷纷 2024-12-16 08:20:09

我不认为您调用带有任何参数的执行方法,是吗?

I don't suppose you call the execute method with any argument, do you?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文