AudioManager setStreamMute() 空指针异常

AudioManager setStreamMute() Null Pointer Exception

我在第 219 行收到空指针异常:amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);。以前没有发生过,今天我第一次看到,我使用的是 Android 5.0。代码在 activity 启动时静音通知声音,在销毁时取消静音。

代码:

private AudioManager amanager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
}

@Override
protected void onResume() {
    super.onResume();
    if(amanager!=null) {
        amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
    }
}

@Override
protected void onPause() {    
    if(amanager!=null) {
        amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
    }
    super.onPause();
}

错误:

java.lang.RuntimeException: Unable to resume activity {com.jemshit.itu/com.jemshit.nfcAttendance.ClassActivity}: java.lang.NullPointerException: Null pointer exception during instruction 'invoke-static {v0}, android.os.IBinder android.media.AudioService$VolumeStreamState$VolumeDeathHandler.access00(android.media.AudioService$VolumeStreamState$VolumeDeathHandler) // method@11492'
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3346)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3377)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728)
            at android.app.ActivityThread.access0(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: java.lang.NullPointerException: Null pointer exception during instruction 'invoke-static {v0}, android.os.IBinder android.media.AudioService$VolumeStreamState$VolumeDeathHandler.access00(android.media.AudioService$VolumeStreamState$VolumeDeathHandler) // method@11492'
            at android.os.Parcel.readException(Parcel.java:1546)
            at android.os.Parcel.readException(Parcel.java:1493)
            at android.media.IAudioService$Stub$Proxy.setStreamMute(IAudioService.java:992)
            at android.media.AudioManager.setStreamMute(AudioManager.java:1466)
            at com.jemshit.nfcAttendance.ClassActivity.onResume(ClassActivity.java:219)
            at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1255)
            at android.app.Activity.performResume(Activity.java:6338)
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3335)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3377)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2728)
            at android.app.ActivityThread.access0(ActivityThread.java:172)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

这是一个 Android 错误,不是您的代码。在您的代码中放置一个 try/catch 块以避免崩溃。