我是一个新来的android.I有一个关于asynctaskbackground的简单代码。它不会编译。我在某些行中有问题。在doinBackground方法。我的代码是:
public class MainActivity extends ActionBarActivity {
ProgressDialog progressBar;
int progressincr = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startTask = (Button) findViewById(R.id.startTaskButton);
startTask.setOnClickListener(startTaskListener);
}
private OnClickListener startTaskListener = new OnClickListener() {
@Override
public void onClick(View v) {
Context context = getApplicationContext();
progressBar = new ProgressDialog(v.getContext());
BackgroundTask testBackgroundTask = new BackgroundTask();
testBackgroundTask.execute(context);
CharSequence text = getString(R.string.mainThreadRunning);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
};
private class BackgroundTask extends AsyncTask < Context, integer, string>{
@Override
protected void onPreExecute() {
CharSequence message = getString(R.string.progressMessage);
progressBar.setCancelable(true);
progressBar.setMessage(message);
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
};
@Override
protected String doInBackground(Context... params) {
for (int i = 0; i < 100; i = i + progressincr) {
try { Thread.sleep(100);}
catch (InterruptedException e) {
e.printStackTrace();}
publishProgress(progressincr);
if(isCancelled()) break;
}
return getString(R.string.backgroundTaskcompleted);
}
protected void onProgressUpdate(Integer...values){
progressBar.incrementProgressBy(progressincr);
}
protected void onPostExecute(String result){
progressBar.dismiss();
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context,result, duration);
toast.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
错误是:
***The method publishProgress(R.integer...) in the type AsyncTask<Context,R.integer,R.string> is not applicable for the arguments (int) MainActivity.java /AsyncTaskBackg/src/com/example/asynctaskbackg line 67 Java Problem***
AND:
***Description Resource Path Location Type
The return type is incompatible with AsyncTask<Context,R.integer,R.string>.doInBackground(Context[]) MainActivity.java /AsyncTaskBackg/src/com/example/asynctaskbackg line 60 Java Problem***
我该如何修理它们?是什么导致的?
答案 0 :(得分:0)
你的通用类型错了。
<Context, integer, string>
必须为<Context, Integer, String>
,因为泛型只接受引用,而不是值类型。此类型integer
和string
也不存在。基元类型为int
,相应的包装类为Integer
。此外,没有String
类型的原始变体,因此它始终以大写字母开头
<..>
中的这三个定义类型定义了返回类型或参数,引用了documentation:An asynchronous task is defined by 3 generic types, called Params, Progress and Result.
,但是如果你修改了类型名称,你将会以正确的顺序使用它们。