这是我的代码:
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
答案 0 :(得分:0)
在使用之前,请检查您是否已初始化AudioManager
am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
以下是tutorialspoint website可能对您有所帮助的更多信息。