Android,出现使我的应用程序崩溃的异常

标签 android exception

我是一个菜鸟,通过制作一个简单的游戏来学习安卓:
用户可以按下几个按钮
我在屏幕上显示一些问题,他必须按其中一个按钮作为答案
当他按下按钮时,我会播放声音。
3 个错误答案,游戏结束。

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

真奇怪,答错 3 次游戏就结束了,所以我运行这段代码:

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

这会将用户送回到上一个 Activity ,他们可以重新开始游戏。 问题每 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);     
}

请告诉我如何解决这个烦人的“bug”。

谢谢!
瑞安

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

编辑:

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

最佳答案

...

编辑:

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.

调用release后就不能再使用soundPool对象了,所以需要新建一个实例。因此,请执行此操作以确保它不会被重新使用:

   soundPool.release();
   soundPool = null;

关于Android,出现使我的应用程序崩溃的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7583932/

相关文章:

android - java.lang.IllegalStateException : ViewTreeLifecycleOwner not found from androidx. constraintlayout.widget.ConstraintLayout

android - Galaxy S 3 内存问题

unit-testing - Racket exn :fail? 未绑定(bind)

c# - 异常记录和发送电子邮件

android - 加载 View 寻呼机时出现奇怪的 OutOfMemoryError

java - android,返回命令并没有真正返回

c# - 在发生异常时将自定义对象传递给 RunWorkerCompleted 事件

java - 为什么新线程立即操作UI时没有CalledFromWrongThreadException?

java - Struts 2 为某些操作添加异常映射

安卓 map 错误: Couldn't get connection factory client (with a correct ApiKey)