我想知道當按鈕被點擊時如何取消定時器任務..我正在做一個簡單的數學遊戲,因爲我已經分配了一個時間任務改變一個TextView的文本,我想取消按鈕時clicked.But我得到了應用程序的時候我這樣做如何在點擊按鈕之前取消定時器任務
public class MainActivity extends Activity implements OnClickListener {
Button b1, b2,b3;
ImageView i, i2, i3;
TextView t1, t2, t3,t4;
String s1, s2, s3, s4, s5;
LinearLayout pooku;
Button bk;
int q, m, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, n[];
Random r1 = new Random();
Random r0 = new Random();
Handler handler = new Handler();
Runnable r;
Timer t = new Timer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
pooku = (LinearLayout) findViewById(R.id.pooka);
i = (ImageView) findViewById(R.id.imageView1);
t1 = (TextView) findViewById(R.id.textView1);
t2 = (TextView) findViewById(R.id.textView2);
t3 = (TextView) findViewById(R.id.textView3);
t4= (TextView) findViewById(R.id.textView4);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
imp();
}
public TimerTask malli(){
return new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
t4.setText("times up");
t4.setVisibility(View.VISIBLE);
}
});
}
};
}
public void imp() {
t.schedule(malli(), 3000);
r2 = r1.nextInt(9) + 1;
r3 = r1.nextInt(9) + 1;
r4 = r2 + r3;
r5 = r2 - r3;
r6 = r2 * r3;
n = new int[] { r4, r5, r6 };
doit();
while (r9 == r11 || r9 == r13 || r11 == r13) {
doit();
}
s1 = Integer.toString(r2);
s2 = Integer.toString(r3);
s3 = Integer.toString(r9);
s4 = Integer.toString(r11);
s5 = Integer.toString(r13);
t1.setText(s1);
t2.setText(s2);
b1.setText(s3);
b2.setText(s4);
b3.setText(s5);
int[] images = { R.drawable.plus, R.drawable.minus, R.drawable.multiply };
Random random = new Random();
m = images[random.nextInt(images.length)];
i.setImageResource(m);
bk.setFocusable(true);
bk.requestFocus();
bk.requestFocusFromTouch();
}
public void doit() {
r9 = 0;
r11 = 0;
r13 = 0;
r8 = r1.nextInt(3) + 0;
r9 = n[r8];
r10 = r1.nextInt(3) + 0;
r11 = n[r10];
r12 = r1.nextInt(3) + 0;
r13 = n[r12];
int[] buttons = { R.id.button1, R.id.button2, R.id.button3 };
q = buttons[r0.nextInt(buttons.length)];
bk = (Button) findViewById(q);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
click(r9, b1);
break;
case R.id.button2:
click(r11, b2);
break;
case R.id.button3:
click(r13, b3);
break;
}
}
public void click(int k, Button b) {
if (m == R.drawable.plus && k == r4 || m == R.drawable.minus && k == r5
|| m == R.drawable.multiply && k == r6) {
t3.setVisibility(View.INVISIBLE);
t4.setVisibility(View.INVISIBLE);
this.t.cancel();
imp();
t4.setVisibility(View.INVISIBLE);
} else {
t3.setText("FINISH");
t3.setVisibility(View.VISIBLE);
b.setPressed(true);
b1.setClickable(false);
b2.setClickable(false);
b3.setClickable(false);
b.requestFocusFromTouch();
}
}
}
和logcat的報告
04-04 16:47:47.681: E/AndroidRuntime(11493): Process: com.example.emo2, PID: 11493
04-04 16:47:47.681: E/AndroidRuntime(11493): java.lang.IllegalStateException: Timer was canceled
04-04 16:47:47.681: E/AndroidRuntime(11493): at java.util.Timer.scheduleImpl(Timer.java:561)
04-04 16:47:47.681: E/AndroidRuntime(11493): at java.util.Timer.schedule(Timer.java:459)
04-04 16:47:47.681: E/AndroidRuntime(11493): at com.example.emo2.MainActivity.imp(MainActivity.java:74)
04-04 16:47:47.681: E/AndroidRuntime(11493): at com.example.emo2.MainActivity.click(MainActivity.java:154)
04-04 16:47:47.681: E/AndroidRuntime(11493): at com.example.emo2.MainActivity.onClick(MainActivity.java:135)
04-04 16:47:47.681: E/AndroidRuntime(11493): at android.view.View.performClick(View.java:4443)
04-04 16:47:47.681: E/AndroidRuntime(11493): at android.view.View$PerformClick.run(View.java:18442)
04-04 16:47:47.681: E/AndroidRuntime(11493): at android.os.Handler.handleCallback(Handler.java:733)
04-04 16:47:47.681: E/AndroidRuntime(11493): at android.os.Handler.dispatchMessage(Handler.java:95)
04-04 16:47:47.681: E/AndroidRuntime(11493): at android.os.Looper.loop(Looper.java:136)
04-04 16:47:47.681: E/AndroidRuntime(11493): at android.app.ActivityThread.main(ActivityThread.java:5021)
04-04 16:47:47.681: E/AndroidRuntime(11493): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 16:47:47.681: E/AndroidRuntime(11493): at java.lang.reflect.Method.invoke(Method.java:515)
04-04 16:47:47.681: E/AndroidRuntime(11493): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:82 7)
04-04 16:47:47.681: E/AndroidRuntime(11493): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
04-04 16:47:47.681: E/AndroidRuntime(11493): at dalvik.system.NativeStart.main(Native Method)
你是什麼意思 「出來的應用程序」? – 2015-04-04 11:19:03
它崩潰說「不幸你的應用程序已停止工作」 – 2015-04-04 11:35:17