AsyncTask 不通用?
当我尝试编译以下代码时,出现两个错误:
描述 资源路径 位置类型 标记“void”上的语法错误,无效表达式 AsyncTask.java /AsyncTask Project/src/org/me/asynctask
第 19 行 Java 问题
描述 资源路径 位置类型 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用错误的类型重写 AsyncTask 方法。它定义为:
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:
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.
有几个问题,其中两个是:
您定义了一个类
AsyncTask
,它与android.os.AsyncTask
隐藏/冲突。也许您打算将其称为 AsyncTaskDemo.java 或 AsyncTaskActivity.java 或其他名称。您正在尝试通过调用
doInBackground
内部的tv.setText
从后台线程更改 UI。这违背了 AsyncTask 的目的。doInBackground
中的代码不应触及主(或“UI”)线程。对于doInBackground
内的中期任务更新,请使用AsyncTask.publishProgress
。There are several problems, two of which are:
You defined a class
AsyncTask
, which hides/collides withandroid.os.AsyncTask
. Maybe you intended to call it AsyncTaskDemo.java or AsyncTaskActivity.java or something.You're trying to change the UI from the background thread by calling
tv.setText
inside ofdoInBackground
. That defeats the purpose ofAsyncTask
. Code indoInBackground
shouldn't be touching the main (or 'UI') thread. For mid-task updates inside ofdoInBackground
, useAsyncTask.publishProgress
.