处理MediaFile应用程序中的异常
我正在开发一个媒体文件应用程序,用于播放存储在我的原始文件夹中的声音。 总共有大约32种声音。 这就是我在点击每个按钮时所做的:
Button btnGrowUp = (Button) this.findViewById(R.id.GrowUp);
btnGrowUp.setOnClickListener(btnGrowUpListener);
private OnClickListener btnGrowUpListener = new OnClickListener()
{
public void onClick(View v)
{
//Toast.makeText(getBaseContext(), "Grow Up audio file is being played", Toast.LENGTH_SHORT).show();
MediaPlayer mp = MediaPlayer.create(MainScreen.this, R.raw.growup);
mp.start();
}
};
我在logcat窗口中看到一个运行时异常,提示“应用程序意外停止”,并显示以下消息:
12-19 12:33:05.420: WARN/dalvikvm(699): threadid=3: thread exiting with uncaught exception (group=0x4000fe70) 12-19 12:33:05.441: ERROR/AndroidRuntime(699): Uncaught handler: thread main exiting due to uncaught exception 12-19 12:33:05.460: ERROR/AndroidRuntime(699): java.lang.NullPointerException 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at com.myapps.media.MainScreen$8.onClick(MainScreen.java:244) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at android.view.View.performClick(View.java:2179) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at android.view.View.onTouchEvent(View.java:3828) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at android.widget.TextView.onTouchEvent(TextView.java:6291) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at android.view.View.dispatchTouchEvent(View.java:3368) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at com.android.internal.policy.impl.PhoneWindow $DecorView.superDispatchTouchEvent(PhoneWindow.java:1707) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent (PhoneWindow.java:1197) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at android.app.Activity.dispatchTouchEvent(Activity.java:1993) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at com.android.internal.policy.impl.PhoneWindow $DecorView.dispatchTouchEvent(PhoneWindow.java:1691) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at android.view.ViewRoot.handleMessage(ViewRoot.java:1525) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at android.os.Handler.dispatchMessage(Handler.java:99) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at android.os.Looper.loop(Looper.java:123) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at android.app.ActivityThread.main(ActivityThread.java:3948) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at java.lang.reflect.Method.invokeNative(Native Method) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at java.lang.reflect.Method.invoke(Method.java:521) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:782) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540) 12-19 12:33:05.460: ERROR/AndroidRuntime(699): at dalvik.system.NativeStart.main(Native Method) 12-19 12:33:05.520: INFO/Process(563): Sending signal. PID: 699 SIG: 3 12-19 12:33:05.520: INFO/dalvikvm(699): threadid=7: reacting to signal 3 12-19 12:33:05.601: INFO/dalvikvm(699): Wrote stack trace to '/data/ anr/traces.txt' 12-19 12:33:05.982: INFO/ARMAssembler(563): generated scanline__00000077:03515104_00000000_00000000 [ 27 ipp] (41 ins) at [0x27c718:0x27c7bc] in 982527 ns 12-19 12:33:06.011: INFO/ARMAssembler(563): generated scanline__00000077:03515104_00001001_00000000 [ 64 ipp] (84 ins) at [0x27c7c0:0x27c910] in 1804978 ns
如何解决这个问题? 谢谢
Maqsood
您使用的MediaPlayer
构造函数的Javadoc说,如果播放器的创建失败,它将返回null
。
在调用start
方法之前,您没有检查创建是否成功,因此根据上面的堆栈跟踪,这是您看到的异常的最可能原因。