Issue
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?
Solution
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.setTextinside ofdoInBackground. That defeats the purpose ofAsyncTask. Code indoInBackgroundshouldn't be touching the main (or 'UI') thread. For mid-task updates inside ofdoInBackground, useAsyncTask.publishProgress.
Answered By - Roman Nurik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.