0

I am developing a simple application in which buttons background change dynamically after one second. I have a button in my app name stopButtonBackgroundChanging, as I click the button it stops the changing of buttons background but after 2-seconds the app Stops Unexpectedly". Please help me in this respect and also see my code below you might get better idea that what's really I want to do.

UpdateRunnable updateRunnable;  
private Runnable mEndlessRunnable;


     public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.myLayout);


            Button stopButtonBackgroundChanging = (Button)findViewById(R.id.buttonStop);
            stopButtonBackgroundChanging.setOnClickListener(new OnClickListener() 
            {

                public void onClick(View v) 
                {
                    //See implementation of stop() method at the end of the code
                    updateRunnable.stop();

                }
            });


            mEndlessRunnable = (Runnable) new UpdateRunnable(new Handler(), new Button[] 
            {

                    (Button) findViewById(R.id.button1),
                    (Button) findViewById(R.id.button2),
                    (Button) findViewById(R.id.button3),
                    (Button) findViewById(R.id.button4),

            });
            mEndlessRunnable.run();






        }// End of onCreate()



     //Start of UpdateRunnable
     private static class UpdateRunnable implements Runnable 
     {

         private boolean myContinue = true;


            private Random mRand = new Random();
            private Handler mHandler;
            private Button[] mButtons;

            private Button mCurButton;
            private int mState;

            public UpdateRunnable(Handler handler, Button[] buttons) 
            {
                mHandler = handler;
                mButtons = buttons;
            }

            public void run() 
            {
                if(myContinue)
                {
                    // select a button if one is not selected
                    if (mCurButton == null) 
                    {
                        mCurButton = mButtons[mRand.nextInt(mButtons.length)];
                    }
                    // check internal state, `0` means first bg change, `1` means last
                    switch (mState) 
                    {
                    case 0:
                        mCurButton.setBackgroundResource(R.drawable.buttonyellow);
                        mState = 1;
                        break;
                    case 1:
                        mCurButton.setBackgroundResource(R.drawable.buttonblue);
                        // reset state and nullify so this continues endlessly
                        mState = 0;
                        mCurButton = null;
                        break;
                    }

                    mHandler.postDelayed(this, 1000); 

                }




            }// End of run()

            public void stop()
            {
             this.myContinue =false;    
            }

        }//End of class UpdateRunnable
user1703737
  • 533
  • 1
  • 11
  • 25

1 Answers1

2

I believe you will get this to work by changing the your onClick:

public void onClick(View v)
{
    //See implementation of stop() method at the end of the code
    ((UpdateRunnable) mEndlessRunnable).stop();
}
vonRogue
  • 61
  • 2
  • @Von rogue your answer copied by gautham see this link.http://stackoverflow.com/questions/16188398/how-to-stop-runnable-on-button-click-in-android – Nikhil Agrawal Apr 24 '13 at 12:15