我正在做一个小游戏,在游戏中有一个随机和一个媒体播放器,
随机给出1-15之间的数字,然后是CountDownTimer(倒计时秒是综合随机数),倒计时结束后,媒体播放器启动,这是我的代码:
final Random rand = new Random();
time = rand.nextInt(15) + 1;
new CountDownTimer(time * 1000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
mPlayer = MediaPlayer.create(MainActivity.this,
R.raw.windows_8_notify);
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
} catch (IOException e) {
}
mPlayer.start();
}
这个代码在我按下按钮时调用,无论如何在按下按钮之后和mPlayer启动之前取消此操作?我的意思是,当我释放按钮时,它将检查“Timecounter”是否等于0,如果是,它将在启动前取消MediaPlayer,这是我的发布代码:
private View.OnTouchListener speakTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View pView, MotionEvent pEvent) {
pView.onTouchEvent(pEvent);
if (pEvent.getAction() == MotionEvent.ACTION_UP) {
if (isSpeakButtonLongPressed) {
if (Timecounter == 0) {
textview.setText("You have released the button before the sound");
// ADD THE mPlayer cancel
} else {
textview.setText("Your Reaction Time:" + Timecounter);
mPlayer.pause();
mPlayer.reset();
}
isSpeakButtonLongPressed = false;
}
}
return false;
}
};
抱歉我的英文:(希望你理解
- 编辑 -
我已将代码更改为:
private CountDownTimer CountDownTimer1;
....
final Random rand = new Random();
time = rand.nextInt(15) + 1;
final CountDownTimer CountDownTimer1 = new CountDownTimer(time * 1000,
1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
mPlayer = MediaPlayer.create(MainActivity.this,
R.raw.windows_8_notify);
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
} catch (IOException e) {
}
mPlayer.start();
Timecounter = 0;
Timer t = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Timecounter++;
}
});
}
};
t.scheduleAtFixedRate(task, 0, 1000);
}
}.start();
isSpeakButtonLongPressed = true;
return true;
........
if (Timecounter == 0) {
textview.setText("You have released the button before the sound");
CountDownTimer1.cancel();
}
但它不起作用,我在logcat上得到nullpointerexception错误: 错误行是计时器取消
08-13 22:24:37.952: E/InputEventReceiver(1824): Exception dispatching input event.
08-13 22:24:37.952: E/MessageQueue-JNI(1824): Exception in MessageQueue callback: handleReceiveCallback
08-13 22:24:37.967: E/MessageQueue-JNI(1824): java.lang.NullPointerException
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at com.time.reactiontime.MainActivity$2.onTouch(MainActivity.java:83)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.View.dispatchTouchEvent(View.java:7708)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2329)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1568)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2277)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.View.dispatchPointerEvent(View.java:7893)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.os.MessageQueue.nativePollOnce(Native Method)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.os.MessageQueue.next(MessageQueue.java:138)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.os.Looper.loop(Looper.java:123)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at android.app.ActivityThread.main(ActivityThread.java:5081)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at java.lang.reflect.Method.invoke(Method.java:515)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
08-13 22:24:37.967: E/MessageQueue-JNI(1824): at dalvik.system.NativeStart.main(Native Method)
08-13 22:24:37.967: D/AndroidRuntime(1824): Shutting down VM
08-13 22:24:37.967: W/dalvikvm(1824): threadid=1: thread exiting with uncaught exception (group=0x41af3ce0)
08-13 22:24:37.975: E/AndroidRuntime(1824): FATAL EXCEPTION: main
08-13 22:24:37.975: E/AndroidRuntime(1824): Process: com.time.reactiontime, PID: 1824
08-13 22:24:37.975: E/AndroidRuntime(1824): java.lang.NullPointerException
08-13 22:24:37.975: E/AndroidRuntime(1824): at com.time.reactiontime.MainActivity$2.onTouch(MainActivity.java:83)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.View.dispatchTouchEvent(View.java:7708)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:24:37.975: E/AndroidRuntime(1824): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2329)
08-13 22:24:37.975: E/AndroidRuntime(1824): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1568)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
08-13 22:24:37.975: E/AndroidRuntime(1824): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2277)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.View.dispatchPointerEvent(View.java:7893)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.os.MessageQueue.nativePollOnce(Native Method)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.os.MessageQueue.next(MessageQueue.java:138)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.os.Looper.loop(Looper.java:123)
08-13 22:24:37.975: E/AndroidRuntime(1824): at android.app.ActivityThread.main(ActivityThread.java:5081)
08-13 22:24:37.975: E/AndroidRuntime(1824): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 22:24:37.975: E/AndroidRuntime(1824): at java.lang.reflect.Method.invoke(Method.java:515)
08-13 22:24:37.975: E/AndroidRuntime(1824): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-13 22:24:37.975: E/AndroidRuntime(1824): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
08-13 22:24:37.975: E/AndroidRuntime(1824): at dalvik.system.NativeStart.main(Native Method)
08-13 22:24:39.788: I/Process(1824): Sending signal. PID: 1824 SIG: 9
08-13 22:27:10.452: D/OpenGLRenderer(1907): Enabling debug mode 0
08-13 22:27:10.545: I/ActivityManager(1907): Timeline: Activity_idle id: android.os.BinderProxy@41daff28 time:246367862
08-13 22:27:12.741: E/InputEventReceiver(1907): Exception dispatching input event.
08-13 22:27:12.741: E/MessageQueue-JNI(1907): Exception in MessageQueue callback: handleReceiveCallback
08-13 22:27:12.756: E/MessageQueue-JNI(1907): java.lang.NullPointerException
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at com.time.reactiontime.MainActivity$2.onTouch(MainActivity.java:83)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.View.dispatchTouchEvent(View.java:7708)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2329)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1568)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2277)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.View.dispatchPointerEvent(View.java:7893)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.os.MessageQueue.nativePollOnce(Native Method)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.os.MessageQueue.next(MessageQueue.java:138)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.os.Looper.loop(Looper.java:123)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at android.app.ActivityThread.main(ActivityThread.java:5081)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at java.lang.reflect.Method.invoke(Method.java:515)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
08-13 22:27:12.756: E/MessageQueue-JNI(1907): at dalvik.system.NativeStart.main(Native Method)
08-13 22:27:12.756: D/AndroidRuntime(1907): Shutting down VM
08-13 22:27:12.756: W/dalvikvm(1907): threadid=1: thread exiting with uncaught exception (group=0x41af3ce0)
08-13 22:27:12.764: E/AndroidRuntime(1907): FATAL EXCEPTION: main
08-13 22:27:12.764: E/AndroidRuntime(1907): Process: com.time.reactiontime, PID: 1907
08-13 22:27:12.764: E/AndroidRuntime(1907): java.lang.NullPointerException
08-13 22:27:12.764: E/AndroidRuntime(1907): at com.time.reactiontime.MainActivity$2.onTouch(MainActivity.java:83)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.View.dispatchTouchEvent(View.java:7708)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-13 22:27:12.764: E/AndroidRuntime(1907): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2329)
08-13 22:27:12.764: E/AndroidRuntime(1907): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1568)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
08-13 22:27:12.764: E/AndroidRuntime(1907): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2277)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.View.dispatchPointerEvent(View.java:7893)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.os.MessageQueue.nativePollOnce(Native Method)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.os.MessageQueue.next(MessageQueue.java:138)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.os.Looper.loop(Looper.java:123)
08-13 22:27:12.764: E/AndroidRuntime(1907): at android.app.ActivityThread.main(ActivityThread.java:5081)
08-13 22:27:12.764: E/AndroidRuntime(1907): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 22:27:12.764: E/AndroidRuntime(1907): at java.lang.reflect.Method.invoke(Method.java:515)
08-13 22:27:12.764: E/AndroidRuntime(1907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
08-13 22:27:12.764: E/AndroidRuntime(1907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
08-13 22:27:12.764: E/AndroidRuntime(1907): at dalvik.system.NativeStart.main(Native Method)
**编辑2 ** 代码:
package com.time.reactiontime;
import com.time.reactiontime.R;
import android.annotation.SuppressLint;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.media.*;
import java.io.*;
import java.util.*;
@SuppressLint("NewApi")
public class MainActivity extends Activity {
private int time;
private int Timecounter = 0;
private TextView textview;
private Button button1;
private boolean isSpeakButtonLongPressed = false;
private MediaPlayer mPlayer;
private CountDownTimer CountDownTimer1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
textview = ((TextView) findViewById(R.id.textview));
button1 = (Button) findViewById(R.id.button1);
button1.setOnLongClickListener(speakHoldListener);
button1.setOnTouchListener(speakTouchListener);
}
private View.OnLongClickListener speakHoldListener = new View.OnLongClickListener() {
@Override
public boolean onLongClick(View pView) {
final Random rand = new Random();
time = rand.nextInt(15) + 1;
final CountDownTimer CountDownTimer1 = new CountDownTimer(
time * 1000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
mPlayer = MediaPlayer.create(MainActivity.this,
R.raw.windows_8_notify);
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
} catch (IOException e) {
}
mPlayer.start();
Timecounter = 0;
Timer t = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Timecounter++;
}
});
}
};
t.scheduleAtFixedRate(task, 0, 1000);
}
}.start();
isSpeakButtonLongPressed = true;
return true;
}
};
private View.OnTouchListener speakTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View pView, MotionEvent pEvent) {
pView.onTouchEvent(pEvent);
if (pEvent.getAction() == MotionEvent.ACTION_UP) {
if (isSpeakButtonLongPressed) {
if (Timecounter == 0) {
if (mPlayer == null) {
} else {
textview.setText("You have released the button before the sound");
CountDownTimer1.cancel();
mPlayer = null;
}
} else {
textview.setText("Your Reaction Time:" + Timecounter);
mPlayer.pause();
mPlayer.reset();
}
isSpeakButtonLongPressed = false;
}
}
return false;
}
};
}
答案 0 :(得分:0)
继续参考计时器并调用cancel()
方法。
根据帖子的编辑2进行编辑
if (isSpeakButtonLongPressed) {
if (mPlayer == null) {
textview.setText("You have released the button before the sound");
CountDownTimer1.cancel();
} else {
textview.setText("Your Reaction Time:" + Timecounter);
mPlayer.pause();
mPlayer.reset();
mPlayer = null;
}
isSpeakButtonLongPressed = false;
}