Basant
Basant

Reputation: 305

Run animation in ViewFlipper to flip images when button click more than one time

I'm working on LCR dice game in Android and I have a problem,when I try to click again on a button to run image flip animation,I'm using Asynctask to run animation this is a sample code of my project:

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;

                }   

}

And this is 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);


        }


    };

And I got this error:

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)

How to solve this problem please help me ?

Upvotes: 0

Views: 909

Answers (1)

Rob R.
Rob R.

Reputation: 3629

First, the error is saying you cannot touch the ViewFlipper unless you were in the same thread that created the ViewFlipper (which is the UI thread I am assuming)

Seems like you might have to use runOnUiThread( Runnable ) to mess with the animations on the view flipper

Upvotes: 1

Related Questions