Handling Exception in a MediaFile app
I'm developing a media file app that is meant to play sounds stored in my raw folder. There are about 32 sounds in all. And this is what i'm doing on the click of each button:
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();
}
};
I get a runtime exception saying "app stops unexpectedly" with the follow messages in my logcat window:
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
How to resolve this issue? Thank you
Maqsood
The Javadoc for the MediaPlayer
constructor you're using says that it will return null
if creation of the player fails.
You're not checking whether creation was successful before calling the start
method, so based on the stack trace above, that's the most likely cause of the exception you're seeing.
上一篇: 在android中点击按钮显示地图
下一篇: 处理MediaFile应用程序中的异常