所以,我正在尝试运行最简单的AsyncTask:它不接受参数,只运行一个函数,获取结果字符串并显示is。无论我尝试什么,我都会得到这个错误:
java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.Void[] at (...)$AsyncTaskRunner.doInBackground
异步任务:
private class AsyncTaskRunner extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
resultText.setText("");
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(Void... params) {
return "test";
}
@Override
protected void onPostExecute(String result) {
progressBar.setVisibility(View.GONE);
resultText.setText(result);
}
}
我就是这样称呼它的:
buttonCalculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (AsyncTaskRunner != null) {
AsyncTaskRunner.cancel(true);
}
AsyncTaskRunner = new AsyncTaskRunner();
AsyncTaskRunner.execute();
}
});
我在这里做错了什么?
好吧,所以奇怪的是,如果我这样称呼它,它会起作用:
AsyncTask<Void, Void, String> asyncTaskRunner = new AsyncTaskRunner();
asyncTaskRunner.execute();