我需要异步播放几个动画,所以我决定用AsyncTask实现AnimationListener。不幸的是,它在我开始制作动画时会抛出错误RuntimeException: An error acuured while executing doInBackground()
。是否可以使用AsyncTask?
代码: 公共类游戏扩展活动{
private static final String TAG = Game.class.getSimpleName();
private Animation animation1;
private Animation animation2;
...
...
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
// animations for turning cards
this.animation1 = AnimationUtils.loadAnimation(this, R.anim.to_middle);
this.animation2 = AnimationUtils.loadAnimation(this, R.anim.from_middle);
...
...
AsyncTurn turn1 = new AsyncTurn(Game.this, position);
turn1.execute();
}
的AsyncTask:
public class AsyncTurn extends AsyncTask<Void,Void,Void> implements AnimationListener{
private Context context;
private int actPosition;
public AsyncTurn(Context context, int position) {
this.context = context;
this.actPosition = position;
animation1.setAnimationListener(this);
animation2.setAnimationListener(this);
}
@Override
protected Void doInBackground(Void... params) {
((ImageView) gridview.getChildAt(card1.getPosition())).clearAnimation();
((ImageView) gridview.getChildAt(card1.getPosition())).setAnimation(animation1);
// ERROR AT NEXT LINE
((ImageView) gridview.getChildAt(card1.getPosition())).startAnimation(animation1);
}
答案 0 :(得分:0)
不知道为什么你需要AsyncTask但你可以使用AsyncTask函数
void onProgressUpdate(Progress... values)
doInBackground()
中的调用函数publishProgrss()
参考:AsyncTask
或使用runOnUIThread 示例:RunOnUiThread Example