我正在开发一个应用程序,它包含一个实现ViewPager和3个片段的MainActivity。我在MainActivity中定义了所有逻辑方法,片段只是实现了UI引用的方法。
因此,在MainActivity中,我有一种方法可以解决问题,并以这种方式启动MediaPlayer:
mp = MediaPlayer.create(MainActivity.this, R.raw.alarm);
try {
mp.setLooping(true);
} catch (IllegalStateException e) {
e.printStackTrace();
}
mp.start();
这会将声音播放到循环中。现在,我需要能够阻止它只是触摸屏幕,因为我已经通过这种方式实现了onTouchEvent()方法:
@Override
public boolean onTouchEvent (MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
/*If sound is playing, stops*/
if (mp.isPlaying()) {
mp.stop();
}
return true;
}
return super.onTouchEvent(event);
}
这就是问题所在。我无法停止声音,所以我认为上面的方法不起作用。一个SO成员让我使用dispatchTouchEvent()
而不是onTouchEvent()
,但这样做就可以关闭app力而不会在LogCat中抛出任何错误消息。
@Override
public boolean dispatchTouchEvent (MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
/*If sound is playing, stops*/
if (mp.isPlaying()) {
mp.stop();
}
return true;
}
return super.dispatchTouchEvent(event);
}
所以我不知道该方法有什么问题。或者问题可能是这必须在每个片段而不是在MainAcitivity中实现?
更新
我遇到了eclipse的问题,它显示了logCat信息,但现在已经解决了,这就是它抛出的内容:
09-01 16:43:15.591: E/InputEventReceiver(30873): Exception dispatching input event.
09-01 16:43:15.591: E/MessageQueue-JNI(30873): Exception in MessageQueue callback: handleReceiveCallback
09-01 16:43:15.591: E/MessageQueue-JNI(30873): java.lang.NullPointerException
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at com.itest.MainActivity.dispatchTouchEvent(MainActivity.java:818)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchTouchEvent(ActionBarActivityDelegateICS.java:260)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2228)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.View.dispatchPointerEvent(View.java:8315)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4596)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4464)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4022)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4076)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4045)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4156)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4053)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4213)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4022)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4076)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4045)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4053)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4022)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6403)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6307)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6278)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6243)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6483)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.os.MessageQueue.nativePollOnce(Native Method)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.os.MessageQueue.next(MessageQueue.java:138)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.os.Looper.loop(Looper.java:123)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at android.app.ActivityThread.main(ActivityThread.java:5356)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at java.lang.reflect.Method.invoke(Method.java:515)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
09-01 16:43:15.591: E/MessageQueue-JNI(30873): at dalvik.system.NativeStart.main(Native Method)
09-01 16:43:15.591: D/AndroidRuntime(30873): Shutting down VM
09-01 16:43:15.601: W/dalvikvm(30873): threadid=1: thread exiting with uncaught exception (group=0x4180bda0)
09-01 16:43:15.601: E/AndroidRuntime(30873): FATAL EXCEPTION: main
09-01 16:43:15.601: E/AndroidRuntime(30873): Process: com.itest, PID: 30873
09-01 16:43:15.601: E/AndroidRuntime(30873): java.lang.NullPointerException
09-01 16:43:15.601: E/AndroidRuntime(30873): at com.itest.MainActivity.dispatchTouchEvent(MainActivity.java:818)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchTouchEvent(ActionBarActivityDelegateICS.java:260)
09-01 16:43:15.601: E/AndroidRuntime(30873): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2228)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.View.dispatchPointerEvent(View.java:8315)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4596)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4464)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4022)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4076)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4045)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4156)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4053)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4213)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4022)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4076)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4045)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4053)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4022)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6403)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6307)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6278)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6243)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6483)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.os.MessageQueue.nativePollOnce(Native Method)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.os.MessageQueue.next(MessageQueue.java:138)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.os.Looper.loop(Looper.java:123)
09-01 16:43:15.601: E/AndroidRuntime(30873): at android.app.ActivityThread.main(ActivityThread.java:5356)
09-01 16:43:15.601: E/AndroidRuntime(30873): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 16:43:15.601: E/AndroidRuntime(30873): at java.lang.reflect.Method.invoke(Method.java:515)
09-01 16:43:15.601: E/AndroidRuntime(30873): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
09-01 16:43:15.601: E/AndroidRuntime(30873): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
09-01 16:43:15.601: E/AndroidRuntime(30873): at dalvik.system.NativeStart.main(Native Method)
MainActivity中的第818行指的是onTouchEvent()或dispatchTouchEvent()方法,正好是这一行:
if (mp.isPlaying()) {
答案 0 :(得分:1)
您正在线上<{1}}
NullPointerException
最初触摸屏幕时, if (mp.isPlaying()) {
必须为空。
添加:
mp
希望这有帮助。