我使用這個標準的SoundManager。它工作正常,我所有的設備,但市場上唯一一個現在,然後我得到這些錯誤在SoundManager.playSound(SoundManager.java:87)SoundManager中的偶爾NullPointerException
NullPointerException異常
的NullPointerException在SoundManager.cleanup(SoundManager類的.java:107)
下面是代碼:
public class SoundManager {
private static SoundManager _instance;
private static SoundPool mSoundPool;
private static HashMap<Integer, Integer> mSoundPoolMap;
private static AudioManager mAudioManager;
private static Context mContext;
private SoundManager(){ }
static synchronized public SoundManager getInstance(){
if (_instance == null)
_instance = new SoundManager();
return _instance;
}
public static void initSounds(Context theContext){
mContext = theContext;
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public static void addSound(int Index,int SoundID){
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}
public static void loadSounds(){
mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.kick1, 1));
mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.kick2, 1));
mSoundPoolMap.put(3, mSoundPool.load(mContext, R.raw.kick3, 1));
}
public static void playSound(int index, float volume){
**line 87:** float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume/mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play(mSoundPoolMap.get(index), streamVolume*volume, streamVolume*volume, 1, 0, 1);
}
public static void stopSound(int index){
mSoundPool.stop(mSoundPoolMap.get(index));
}
public static void cleanup(){
**line 107:** mSoundPool.release();
mSoundPool = null;
mSoundPoolMap.clear();
mAudioManager.unloadSoundEffects();
_instance = null;
}
}
這是清理通話是在開始活動:
//REMOVE SOUND MEMORY ALLOCATION
@Override
public void onDestroy()
{
super.onDestroy();
SoundManager.cleanup();
}
有誰知道這可能是導致這些偶然的罕見錯誤,以及如何預防呢?這發生在我使用SoundManager的所有應用程序中......即使有點炒作也可能有所幫助。
如果您能夠重現錯誤,請在'playSound'和'cleanup'方法中記錄'mSoundPool','mSoundPoolMap'和'mAudioManager'的值。其中一個肯定是空的。不過,我猜想,在某些情況下,在'initSounds'之前調用了某些東西。 – Eric 2012-08-11 22:00:15
謝謝埃裏克。我無法在我的設備上重現錯誤,否則找到原因會更容易。 – Lumis 2012-08-15 09:58:11
你能標記線條87和107嗎? – WarrenFaith 2012-08-15 10:02:37