我正在为我的大学制作一个项目。但我尝试在半小时内学习AsyncTask。硬! 我制作了一个游戏" Simon"我需要一个接一个地闪烁一秒钟的颜色。但我不理解错误以及解决方法......
对不起我的英语,我是巴西人。
这是我的AsyncTask类......
package com.example.genius;
import android.os.AsyncTask;
import android.widget.ImageView;
public class Queue extends AsyncTask<Void, Void, ImageView> {
protected ImageView doInBackground(Void... params) {
ImageView imagem;
int i = GameActivity.numCor;
imagem = GameActivity.cor;
if(i == 0)
imagem.setImageResource(R.drawable.greenlight);
if(i == 1)
imagem.setImageResource(R.drawable.redlight);
if(i == 2)
imagem.setImageResource(R.drawable.yellowlight);
if(i == 3)
imagem.setImageResource(R.drawable.bluelight);
return imagem;
}
protected ImageView onPostExecute() {
ImageView imagem;
int i = GameActivity.numCor;
imagem = GameActivity.cor;
try {
Thread.sleep(1000);
}catch(Exception e) {}
if(i == 0)
imagem.setImageResource(R.drawable.green);
if(i == 1)
imagem.setImageResource(R.drawable.red);
if(i == 2)
imagem.setImageResource(R.drawable.yellow);
if(i == 3)
imagem.setImageResource(R.drawable.blue);
return imagem;
}
}
和我的GameActivity的方法,我称之为AsyncTask类......
public void PiscarVerde() {
numCor = 0;
cor = (ImageView)findViewById(R.id.green);
Queue queue = new Queue();
final MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.gr);
mediaPlayer.start();
queue.execute();
}
可以帮我这个吗?感谢&#39; S
答案 0 :(得分:1)
您的onPostExecute签名错误,请更改:
protected ImageView onPostExecute() {
到
protected void onPostExecute(ImageView result) {
你还真的写错了什么,问题出在哪里
顺便说一句。在Java中,一个好的做法是在覆盖方法时添加@Override注释,编译器会在出错时输出错误,例如:
@Override
protected void onPostExecute(ImageView result) {
答案 1 :(得分:0)
=您的onPostExecute签名错误,更改:
受保护的ImageView onPostExecute(){..}
到
protected void onPostExecute(ImageView imagem){..}
AsyncTask使用起来并不复杂,你有3个主要步骤:
//You have to do some init if needed
protected void onPreExecute() {
super.onPreExecute();
}
//The background task do your async stuff
protected ImageView doInBackground(Void... params) {
return null;
}
//In this part do stuff to update your UI
protected void onPostExecute(ImageView imageView) {
super.onPostExecute(imageView);
}
答案 2 :(得分:0)
您的onPostExecute需要返回void。您可能还想使用对Android开发人员推荐的ImageView的弱引用。您可以在这里查看它是如何完成的: Link to Android Developers