来自MediaPlayer onPause()的NullPointerException丢失了引用

时间:2015-04-13 15:58:34

标签: android android-mediaplayer

退出活动时,我通过onPause()方法从MediaPlayer获取NullPointerException。奇怪的是我没有从LogCat得到错误 - 如果(mp == null)Log.i(TAG,“mp为null”)我必须这样做;并记录到LogCat。退出声音后播放几秒钟然后停止播放。

我从onPause()调用此方法:

void releaseThis(){

    if(mp == null) Log.i("ASD", "nie ma mediaplayera");
    if (mp != null) {
        if(mp.isPlaying()){
            mp.stop();
        }
        Log.i("ASD", "a tu już jest mediaplayer");
        mp.reset();
        mp.release();
        mp = null;
    }
    if(wl.isHeld()){
        wl.release();
    }
}

基础架构:

public class CallScreen extends Activity{
    MediaPlayer mp;
    private PowerManager.WakeLock wl = null;

    onCreate(){
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "My Tag");
        wl.acquire();
    Uri notification =  RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);

        playSound(getApplicationContext(), notification);
    }

    playSound(context, alert){
    (here's the beef)
    }
}

日志:

    04-13 14:55:29.934 32338 32338 E AndroidRuntime: java.lang.RuntimeException: Unable to pause activity {com.klangstudios.fakecall/com.klangstudios.fakecall.CallScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.PowerManager$WakeLock.isHeld()' on a null object reference

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3312)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3259)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3234)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at android.app.ActivityThread.access$1000(ActivityThread.java:148)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:102)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:135)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:5312)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Method.java:372)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

04-13 14:55:29.934 32338 32338 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.PowerManager$WakeLock.isHeld()' on a null object reference

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at com.klangstudios.fakecall.CallScreen.releaseThisShit(CallScreen.java:94)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at com.klangstudios.fakecall.CallScreen.onPause(CallScreen.java:106)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at android.app.Activity.performPause(Activity.java:6064)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1317)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3292)

04-13 14:55:29.934 32338 32338 E AndroidRuntime:    ... 11 more

04-13 14:55:51.658 32581 32581 E AndroidRuntime: FATAL EXCEPTION: main

04-13 14:55:51.658 32581 32581 E AndroidRuntime: Process: com.klangstudios.fakecall, PID: 32581

04-13 14:55:51.658 32581 32581 E AndroidRuntime: java.lang.RuntimeException: Unable to pause activity {com.klangstudios.fakecall/com.klangstudios.fakecall.CallScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.PowerManager$WakeLock.isHeld()' on a null object reference

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3312)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3259)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3234)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at android.app.ActivityThread.access$1000(ActivityThread.java:148)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:102)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:135)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:5312)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Method.java:372)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

04-13 14:55:51.658 32581 32581 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.PowerManager$WakeLock.isHeld()' on a null object reference

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at com.klangstudios.fakecall.CallScreen.releaseThisShit(CallScreen.java:94)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at com.klangstudios.fakecall.CallScreen.onPause(CallScreen.java:106)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at android.app.Activity.performPause(Activity.java:6064)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1317)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3292)

04-13 14:55:51.658 32581 32581 E AndroidRuntime:    ... 11 more

04-13 15:19:15.822 10261 10261 E AndroidRuntime: FATAL EXCEPTION: main

04-13 15:19:15.822 10261 10261 E AndroidRuntime: Process: com.klangstudios.fakecall, PID: 10261

04-13 15:19:15.822 10261 10261 E AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {com.klangstudios.fakecall/com.klangstudios.fakecall.CallScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2995)

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3030)

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1336)

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:102)

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:135)

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:5312)

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Method.java:372)

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

04-13 15:19:15.822 10261 10261 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    at com.klangstudios.fakecall.CallScreen.onResume(CallScreen.java:106)

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1264)

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    at android.app.Activity.performResume(Activity.java:6039)

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2978)

04-13 15:19:15.822 10261 10261 E AndroidRuntime:    ... 9 more

04-13 15:39:49.540 16631 16646 F libc    : Fatal signal 11 (SIGSEGV), code 1, fault addr 0xb4e84e56 in tid 16646 (Binder_1)

3 个答案:

答案 0 :(得分:1)

现在有效。你只需要从onCreate()中删除playSound()并将它放到onResume(),因为在Activity到达前台之前总是调用onResume()。

参考:http://developer.android.com/reference/android/app/Activity.html

答案 1 :(得分:0)

04-13 14:55:29.934 32338 32338 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.os.PowerManager$WakeLock.isHeld()' on a null object reference

如果您在这里仔细阅读,可以看到问题出在isHeld()

只需在wl上添加支票,就像在mp上一样。

这样的东西
if(wl != null){
    if(wl.isHeld()){
        wl.release();
    }
}

答案 2 :(得分:0)

快速解答:

if (mediaPlayer != null && mediaPlayer.isPlaying()) {

        //stop music
        mediaPlayer.stop();
        mediaPlayer.reset();
        }