Android,获得一个崩溃我的应用程序的异常

时间:2011-09-28 13:24:18

标签: android exception

我通过制作一个简单的游戏来学习机器人: 用户可以按几个按钮 我在屏幕上显示一些问题,他必须按下其中一个按钮作为答案 当他按下按钮时,我发出声音 3个错误的答案,游戏结束了。

简单吧? 一切正常,我学到了很多,除了一个“#%#”%“#异常让我的应用程序崩溃:(

这真的很奇怪,游戏结束的3个错误答案所以我运行这段代码:

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

将用户发送回上一个活动,他们可以再次开始游戏。 每隔3-5次就会出现问题,他们重新启动游戏...我在一个强制关闭,这在logcat: http://imageshack.us/photo/my-images/90/91939698.png/

错误似乎在这里:

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);     
}

请告诉我如何粉碎这个恼人的“虫子”。

谢谢!
莱恩

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

编辑:

在onCreate中我这样做:

new LoadMusicInBackground().execute();

和LoadMusicInBackground是这样的:

/** 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(); 
    } 
  } 

3 个答案:

答案 0 :(得分:1)

...

编辑:

  

public final void release()
  释放SoundPool资源。释放所有内存和本机   SoundPool对象使用的资源。 SoundPool不再是   used,引用应设置为null。

一旦调用release,就无法重用soundPool对象,因此需要创建一个新实例。所以这样做是为了确保它没有被重新开始:

   soundPool.release();
   soundPool = null;

答案 1 :(得分:0)

我会尝试记录soundPool和soundPoolMap,因为看起来其中一个是null 也许作为重新启动游戏的一部分,这些值被清除了?

答案 2 :(得分:0)

对于 noob ,你做得很好:-D

我认为这会导致您的问题:

soundPool.release();

您可能正在发布它,并尝试下次重复使用它。请参阅release()文档....