Android Media Player后退按钮空指针崩溃

时间:2012-05-03 19:00:00

标签: android nullpointerexception android-mediaplayer

我的应用程序的其余部分运行得非常好,直到按下后退按钮停止并退出程序。我不知道它是代码行的这一部分还是在这里,但这里是DDMS logcat,下面是后退按钮编码。任何帮助将不胜感激。

05-03 18:32:35.597: W/KeyCharacterMap(475): No keyboard for id 0
05-03 18:32:35.597: W/KeyCharacterMap(475): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-03 18:32:35.667: D/AndroidRuntime(475): Shutting down VM
05-03 18:32:35.677: W/dalvikvm(475): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-03 18:32:35.736: E/AndroidRuntime(475): FATAL EXCEPTION: main
05-03 18:32:35.736: E/AndroidRuntime(475): java.lang.NullPointerException
05-03 18:32:35.736: E/AndroidRuntime(475):  at com.mrj05hua.skylanders.SkylandersSoundboardActivity.onBackPressed(SkylandersSoundboardActivity.java:344)
05-03 18:32:35.736: E/AndroidRuntime(475):  at android.app.Activity.onKeyUp(Activity.java:1888)
05-03 18:32:35.736: E/AndroidRuntime(475):  at android.view.KeyEvent.dispatch(KeyEvent.java:1061)
05-03 18:32:35.736: E/AndroidRuntime(475):  at android.app.Activity.dispatchKeyEvent(Activity.java:2068)
05-03 18:32:35.736: E/AndroidRuntime(475):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
05-03 18:32:35.736: E/AndroidRuntime(475):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
05-03 18:32:35.736: E/AndroidRuntime(475):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
05-03 18:32:35.736: E/AndroidRuntime(475):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
05-03 18:32:35.736: E/AndroidRuntime(475):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 18:32:35.736: E/AndroidRuntime(475):  at android.os.Looper.loop(Looper.java:123)
05-03 18:32:35.736: E/AndroidRuntime(475):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-03 18:32:35.736: E/AndroidRuntime(475):  at java.lang.reflect.Method.invokeNative(Native Method)
05-03 18:32:35.736: E/AndroidRuntime(475):  at java.lang.reflect.Method.invoke(Method.java:521)
05-03 18:32:35.736: E/AndroidRuntime(475):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-03 18:32:35.736: E/AndroidRuntime(475):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-03 18:32:35.736: E/AndroidRuntime(475):  at dalvik.system.NativeStart.main(Native Method)
05-03 18:32:39.327: I/Process(475): Sending signal. PID: 475 SIG: 9
05-03 18:36:59.016: D/dalvikvm(484): GC_EXTERNAL_ALLOC freed 873 objects / 61256 bytes in 96ms
05-03 18:36:59.316: D/dalvikvm(484): GC_EXTERNAL_ALLOC freed 276 objects / 13632 bytes in 70ms
05-03 18:36:59.626: D/dalvikvm(484): GC_EXTERNAL_ALLOC freed 139 objects / 7040 bytes in 82ms
05-03 18:36:59.936: D/dalvikvm(484): GC_EXTERNAL_ALLOC freed 88 objects / 4552 bytes in 78ms
05-03 18:37:00.416: D/dalvikvm(484): GC_EXTERNAL_ALLOC freed 96 objects / 5184 bytes in 92ms
05-03 18:37:00.826: D/dalvikvm(484): GC_EXTERNAL_ALLOC freed 101 objects / 5520 bytes in 90ms
05-03 18:37:35.257: W/KeyCharacterMap(484): No keyboard for id 0
05-03 18:37:35.257: W/KeyCharacterMap(484): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-03 18:37:35.336: D/AndroidRuntime(484): Shutting down VM
05-03 18:37:35.349: W/dalvikvm(484): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-03 18:37:35.427: E/AndroidRuntime(484): FATAL EXCEPTION: main
05-03 18:37:35.427: E/AndroidRuntime(484): java.lang.NullPointerException
05-03 18:37:35.427: E/AndroidRuntime(484):  at com.mrj05hua.skylanders.SkylandersSoundboardActivity.onBackPressed(SkylandersSoundboardActivity.java:344)
05-03 18:37:35.427: E/AndroidRuntime(484):  at android.app.Activity.onKeyUp(Activity.java:1888)
05-03 18:37:35.427: E/AndroidRuntime(484):  at android.view.KeyEvent.dispatch(KeyEvent.java:1061)
05-03 18:37:35.427: E/AndroidRuntime(484):  at android.app.Activity.dispatchKeyEvent(Activity.java:2068)
05-03 18:37:35.427: E/AndroidRuntime(484):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
05-03 18:37:35.427: E/AndroidRuntime(484):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
05-03 18:37:35.427: E/AndroidRuntime(484):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
05-03 18:37:35.427: E/AndroidRuntime(484):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
05-03 18:37:35.427: E/AndroidRuntime(484):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 18:37:35.427: E/AndroidRuntime(484):  at android.os.Looper.loop(Looper.java:123)
05-03 18:37:35.427: E/AndroidRuntime(484):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-03 18:37:35.427: E/AndroidRuntime(484):  at java.lang.reflect.Method.invokeNative(Native Method)
05-03 18:37:35.427: E/AndroidRuntime(484):  at java.lang.reflect.Method.invoke(Method.java:521)
05-03 18:37:35.427: E/AndroidRuntime(484):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-03 18:37:35.427: E/AndroidRuntime(484):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-03 18:37:35.427: E/AndroidRuntime(484):  at dalvik.system.NativeStart.main(Native Method)

-

       @Override 
  public void onBackPressed(){
     //Handles back button to close MediaPlayer
        if (player.isPlaying()) {
            super.finish();
        }
        else if (player.isPlaying())
              player.stop();
          player.release();
          super.finish();
  }

1 个答案:

答案 0 :(得分:1)

好吧,对于一个你有一个冗余的if语句

if (player.isPlaying()) 
...
else if (player.isPlaying())
...

您正在检查这两个陈述中的相同内容

接下来,您获取空指针的原因是因为当您尝试访问它时player可能为null

确保在访问播放器之前检查播放器是否为空

@Override 
public void onBackPressed(){
  if (player != null){
      if(player.isPlaying())
          player.stop();

      player.release();
  }

  //there is no reason to call super.finish(); here
  //call super.onBackPressed(); and it will finish that activity for you
  super.onBackPressed(); 
}