AudioManager setStreamMute()空指针异常

时间:2015-06-07 10:12:47

标签: android nullpointerexception android-audiomanager

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

代码:

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.access$5300(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.access$900(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.access$5300(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.access$900(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)

1 个答案:

答案 0 :(得分:4)

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