Я работаю над LCR-игрой в кости на Android, и у меня возникает проблема, когда я пытаюсь снова нажать кнопку для запуска анимации с переворотом изображения, я использую Asynctask
для запуска анимации, это пример кодамой проект:
public class FlippingImage extends AsyncTask<Void, Long, Long>
{
@Override
protected Long doInBackground(Void... params) {
// TODO Auto-generated method stub
int timer=0;
mp=MediaPlayer.create(LCRDiceActivity.this,R.raw.rollingdices);
mp.start();
while (timer<5000)
{
mp.start();
Log.i("LCRgame", "start flip");
mFlipper.startFlipping();
mFlipper.setInAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_in));
mFlipper.setOutAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_out));
mFlipper2.startFlipping();
mFlipper2.setInAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_in));
mFlipper2.setOutAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_out));
mFlipper3.startFlipping();
mFlipper3.setInAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_in));
mFlipper3.setOutAnimation(AnimationUtils.loadAnimation(LCRDiceActivity.this, R.anim.push_up_out));
SystemClock.sleep(1000);
timer=timer+1000;
}
return null;
}
@Override
protected void onProgressUpdate(Long... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
LCRDiceActivity.this.ChipsCountPlayer1.setText(ChipsCount1);
}
@Override
protected void onPostExecute(Long result) {
// TODO Auto-generated method stub
//mFlipper.stopFlipping();
Random myRondom=new Random();
int myNumber=myRondom.nextInt(5)+1;
switch (myNumber) {
case 1:
mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_L_1)));
ChipsCount1=ChipsCount1-1;
break;
case 2:
mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_C_1)));
ChipsCount1=ChipsCount1-1;
break;
case 3:
mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_R_1)));
ChipsCount1=ChipsCount1-1;
break;
case 4:
mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_dot_1)));
break;
case 5:
mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_dot_1)));
break;
case 6:
mFlipper.setDisplayedChild(mFlipper.indexOfChild(findViewById(R.id.img_dot_1)));
break;
default:
break;
}
}
И это buttonClickListener:
private OnClickListener StartClickListener=new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// flipImage=new FlippingImage();
// flipImage.execute( );
Runnable myRunner = new Runnable(){
public void run() {
new FlippingImage().execute();
}};
myHandler=new Handler();
myHandler.post(myRunner);
}
};
И я получил эту ошибку:
11-21 14:16:25.861: ERROR/AndroidRuntime(329): FATAL EXCEPTION: AsyncTask #2
11-21 14:16:25.861: ERROR/AndroidRuntime(329): java.lang.RuntimeException: An error occured while executing doInBackground()
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.os.AsyncTask$3.done(AsyncTask.java:200)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.FutureTask.run(FutureTask.java:138)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at java.lang.Thread.run(Thread.java:1019)
11-21 14:16:25.861: ERROR/AndroidRuntime(329): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.view.ViewRoot.invalidateChild(ViewRoot.java:642)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:668)
11-21 14:16:25.861: ERROR/AndroidRuntime(329):at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
Какрешить эту проблему, пожалуйста, помогите мне?