• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Handler question

vasanthj

Lurker
I have a code like this:

private void startHandler(){
final Handler handler = new Handler();
final Runnable r = new Runnable()
{
public void run()
{
TableLayout tableLayout = (TableLayout)findViewById(R.id.TableLayout01);
if(count >= 0){
if(count == 0){
count = 3;
rows--;
}
if(rows < 0)
handler.removeCallbacks(this);
TableRow tr = (TableRow)tableLayout.getChildAt(rows);
count--;
tr.removeViewAt(count);
}
if(rows == 0 && count == 0)
handler.removeCallbacks(this);
else
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(r, 1000);
}

When the handler.postDelayed(r, 1000) is called, it executes the Runnable. I want to stop the Runnable after sometime when a certain condition is met. For example, in the above code, I want to stop the execution of the Runnable when "rows==0 && count==0". Am I calling handler.removeCallbacks(this) in the correct place? I get an exception when I run this code.
 
Which exception do you get? Would make it easier to help :-)

And just a general note, when posting with code/examples, it makes it a lot easier for others to look at and help if you tab-indent you code in some way. And in this forum you can ad *[*CODE*]* *[*CODE*]* (without the stars) around your code, to make it even easier to read. Just a note :-)

And welcome ;-)
 
Back
Top Bottom