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.
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.
