请求音频焦点时出现空指针异常

时间:2017-09-01 11:24:12

标签: android

这是我的代码:

int result = am.requestAudioFocus(audio_listener,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
    mediaPlayer = MediaPlayer.create(NumbersActivity.this,onwhich_isClickedN.getAudiofile());
    mediaPlayer.start();
    mediaPlayer.setOnCompletionListener(on_comp);
    }

这是错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.AudioManager.requestAudioFocus(android.media.AudioManager$OnAudioFocusChangeListener, int, int)' on a null object reference

1 个答案:

答案 0 :(得分:0)

在使用之前,请检查您是否已初始化AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);

以下是tutorialspoint website可能对您有所帮助的更多信息。