AsyncTask 不通用?

发布于 2024-09-10 13:15:01 字数 1716 浏览 1 评论 0原文

当我尝试编译以下代码时,出现两个错误:

描述 资源路径 位置类型 标记“void”上的语法错误,无效表达式 AsyncTask.java /AsyncTask Project/src/org/me/asynctask 第 19 行 Java 问题

描述 资源路径 位置类型 AsyncTask 类型不是通用的;它不能使用参数 进行参数化; AsyncTask.java /AsyncTask Project/src/org/me/asynctask 第 25 行 Java 问题

package org.me.asynctask;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.os.*;
public class AsyncTask extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView mytextview = new TextView(this);
        TextView secondtextview = new TextView(this);

        new MyAsyncClass().execute(mytextview, void, secondtextview);
    }

    private class MyAsyncClass extends AsyncTask<TextView, Void, Void> {
    protected TextView doInBackground(TextView tv)
    {
        tv.setText("Your AsyncTask was successful!");
        return tv;
    }

    protected void onPostExecute(TextView Result)
    {
        Context context = context.getApplicationContext();
        Toast mytoast = Toast.makeText(context, "AsyncTask processing complete", Toast.LENGTH_LONG);
        mytoast.show();
    }
}

显然 AsyncTask 是一个泛型(http://developer.android.com/reference/android/os/AsyncTask.html#execute(Params.. .) 那么为什么我会收到这些错误呢?

When I try to compile to following code, I get two errors:

Description Resource Path Location Type
Syntax error on token "void", invalid Expression AsyncTask.java /AsyncTask Project/src/org/me/asynctask line 19 Java Problem

Description Resource Path Location Type
The type AsyncTask is not generic; it cannot be parameterized with arguments <TextView, Void, Void> AsyncTask.java /AsyncTask Project/src/org/me/asynctask line 25 Java Problem

package org.me.asynctask;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import android.os.*;
public class AsyncTask extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView mytextview = new TextView(this);
        TextView secondtextview = new TextView(this);

        new MyAsyncClass().execute(mytextview, void, secondtextview);
    }

    private class MyAsyncClass extends AsyncTask<TextView, Void, Void> {
    protected TextView doInBackground(TextView tv)
    {
        tv.setText("Your AsyncTask was successful!");
        return tv;
    }

    protected void onPostExecute(TextView Result)
    {
        Context context = context.getApplicationContext();
        Toast mytoast = Toast.makeText(context, "AsyncTask processing complete", Toast.LENGTH_LONG);
        mytoast.show();
    }
}

Obviously AsyncTask IS a generic (http://developer.android.com/reference/android/os/AsyncTask.html#execute(Params...) so why do I get those errors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

泪是无色的血 2024-09-17 13:15:01

您正在使用错误的类型重写 AsyncTask 方法。它定义为:

AsyncTask<Params, Progress, Result>

doInBackground 方法定义为
protected abstract Result doInBackground (Params... params)

所以根据你的定义,你应该有
protected Void doInBackround(TextView... params);

onPostExecute 的定义也不正确。

You are overriding the AsyncTask methods with the wrong types. It is defined as:

AsyncTask<Params, Progress, Result>

and the doInBackground method is defined as
protected abstract Result doInBackground (Params... params)

so according to your definition, you should have
protected Void doInBackround(TextView... params);

onPostExecute is also incorrectly defined.

奢欲 2024-09-17 13:15:01

有几个问题,其中两个是:

  1. 您定义了一个类 AsyncTask,它与 android.os.AsyncTask 隐藏/冲突。也许您打算将其称为 AsyncTaskDemo.java 或 AsyncTaskActivity.java 或其他名称。

  2. 您正在尝试通过调用 doInBackground 内部的 tv.setText 从后台线程更改 UI。这违背了 AsyncTask 的目的。 doInBackground 中的代码不应触及主(或“UI”)线程。对于 doInBackground 内的中期任务更新,请使用 AsyncTask.publishProgress

There are several problems, two of which are:

  1. You defined a class AsyncTask, which hides/collides with android.os.AsyncTask. Maybe you intended to call it AsyncTaskDemo.java or AsyncTaskActivity.java or something.

  2. You're trying to change the UI from the background thread by calling tv.setText inside of doInBackground. That defeats the purpose of AsyncTask. Code in doInBackground shouldn't be touching the main (or 'UI') thread. For mid-task updates inside of doInBackground, use AsyncTask.publishProgress.

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