1 つの停止ボタンで両方のランナブルを停止しようとすると問題が発生します。2 つのうちの 1 つだけを停止することはできますが、両方を停止しようとするとすぐに、電話でボタンが押されたときにアプリがフリーズします。これまでの私のコード:
if(go != 1){
go = 1;
final Timer t =new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if(DHPDPS==0){
money = (DPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
}else if(counter > DHPDPS && DOTPPS != 0 && DHPDPS != 0){
money = (DOTPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
} else{
money = (DPPS+Reserve);
Reserve = (money);
String end = String.format("%1f", money);
t1.setText("$" + end);
}
//Place your stopping condition over here. Its important to have a stopping condition or it will go in an infinite loop.
counter++;
// Display pay per second
if(counter <= DHPDPS || DHPDPS == 0){
t2.setText("Your pay per second is: $"+result);
}else{
t2.setText("Your pay per second is: $"+result2);
}
}
});
}
}, 20, 20);
// Make countdown to overtime display
final TextView count = (TextView) findViewById(R.id.countdown);
countdown = (int)HPDPS;
cd.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run(){
int hours = (countdown/3600);
if(OTPPS != 0 && HPDPS != 0){
count.setText("Seconds Remaining to Overtime: " + countdown + "\nAbout " + hours + " Hours");
countdown--;
}
}
});
}
}, 1000, 1000);
final Button b = (Button) findViewById(R.id.clockout);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(go == 1)
go = 0;
if (t != null)
t.cancel();
// if (cd != null) // This condition Freezes my phone when activated?
// cd.cancel();
}
});
}
}
どんな助けでも大歓迎です!ありがとう。