开发者

Android, getting an exception that is crashing my app

I am a noob learning android by making a simple game:

There are a few buttons that the user can press

I show some questions on the screen and he has to press one of the buttons as the answer

when he presses the button I play a sound.

3 wrong answers and the game is over.

Simple right?

Everything works and I have learned a lot except for one "#%#"%"# exception that keeps crashing my app :(

It's really strange, on 3 wrong answers the game is over so I run this code:

soundPool.release();            
this.finish();

which sends the user back to the last activity and they can start the game again. The problem arises every 3-5 times that they restart the 开发者_StackOverflow社区game... I get a force close and this in logcat: http://imageshack.us/photo/my-images/90/91939698.png/

The error seems to be here:

public void playSound(int sound) {

AudioManager mgr = (AudioManager)GameScreen.this.getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);    
float volume = streamVolumeCurrent / streamVolumeMax;

/** The below line is line 117 that is crashing the program */
soundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);     
}

Please advise on how I can crush this annoying "bug".

Thanks!

Ryan

================================================================================

EDIT:

in onCreate I am doing this:

new LoadMusicInBackground().execute();

and LoadMusicInBackground is this:

/** Helper class to load all the music in the background. */
class LoadMusicInBackground extends AsyncTask<Void, String, Void> 
{ 
    @Override 
    protected Void doInBackground(Void... unused) { 

        soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 100);
        soundPoolMap = new HashMap<Integer, Integer>();

            soundPoolMap.put(A1,    soundPool.load(GameScreen.this, R.raw.a,    1));                
            soundPoolMap.put(A3,    soundPool.load(GameScreen.this, R.raw.b,    1));         
            soundPoolMap.put(A5,    soundPool.load(GameScreen.this, R.raw.c_s,  1));
            soundPoolMap.put(A6,    soundPool.load(GameScreen.this, R.raw.d,    1));       
            soundPoolMap.put(A8,    soundPool.load(GameScreen.this, R.raw.e,    1)); 
            soundPoolMap.put(A10,   soundPool.load(GameScreen.this, R.raw.f_s,  1)); 
            soundPoolMap.put(A12,   soundPool.load(GameScreen.this, R.raw.g_s,  1));
            soundPoolMap.put(wrong, soundPool.load(GameScreen.this, R.raw.wrong2,   1));

            publishProgress(""); 
        Log.v("SOUNDPOOL",""+soundPoolMap);
      return(null); 
    } 

    @Override 
    protected void onProgressUpdate(String... item) 
    { 
        //text1.setText(item[0]);
    } 

    @Override 
    protected void onPostExecute(Void unused) { 
      //Toast .makeText(GameScreen.this, "Done!", Toast.LENGTH_SHORT).show(); 
    } 
  } 


...

EDIT:

public final void release()
Release the SoundPool resources. Release all memory and native resources used by the SoundPool object. The SoundPool can no longer be used and the reference should be set to null.

You cannot reuse soundPool object once you call release, so you need to create a new instance. So do this to make sure it is not resued:

   soundPool.release();
   soundPool = null;


I'd try logging soundPool and soundPoolMap as it looks like one of these are null.
Perhaps as part of restarting the game these values are cleared?


For a noob you're doing quite well :-D

I think this is causing your problem:

soundPool.release();

You're probably releasing it and try to reuse it next time. See release() documentation....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜