的Android,越来越被撞毁我的应用程序异常

问题描述:

我是一个小白通过制作一个简单的游戏学习的android:
有迹象表明,用户可以按
我在屏幕上显示的一些问题,几个按钮,他要按下其中一个按钮作为答案
当他按下按钮我播放声音。
3错误的答案和游戏结束。的Android,越来越被撞毁我的应用程序异常

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

这真的很奇怪,在3个错误的例外回答游戏就结束了,所以我运行此代码:

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

发送该用户返回到最后一个活动,他们可以重新开始游戏 的问题,每3-5次出现,他们重新开始比赛......我得到一个强制关闭,这在logcat的: http://imageshack.us/photo/my-images/90/91939698.png/

的错误似乎ŧ o在这里:

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

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

+0

这就是我不明白,如果多数民众赞成的情况下,为什么它的工作原理4-5次,每次在此之前发生了什么? – Ryan

+0

它可能会像赛跑状况那样怪异。你有没有明确设置soundPool或soundPoolMap为空? – Jim

+0

好了,编辑上面添加更多的代码,根据那里的日志语句,后台线程并没有开始4-5次,所以soundpool变为null。 – Ryan

对于小白你做的相当不错:-D

我认为这是造成您的问题:

soundPool.release(); 

你可能会释​​放出来,并尝试下一次再利用。见release()文档....

+0

谢谢! :)拿出发布也给我同样的错误:( – Ryan

+0

好吧,这是值得一试: - 我会继续寻找... – Knickedi

+0

我可以上传我的代码,如果你想,它不是一个大秘密或任何东西,只是一个新手项目:) – Ryan

...

编辑:

公众最终无效释放()
松开的Soundpool资源。释放SoundPool对象使用的所有内存和本地 资源。 SoundPool不能再使用 ,并且引用应该设置为null。

一旦你调用release,你就不能重用soundPool对象,所以你需要创建一个新的实例。所以这样做是为了确保它不resued:

soundPool.release(); 
    soundPool = null; 
+0

这样做只是照常进行,同时给我一个空指针异常(但不会崩溃)。编辑OP来添加更多代码,根据日志语句,后台线程并没有开始4-5次,因此soundpool变为空。 :( – Ryan

+0

请检查我的编辑 – Caner

+0

不是,在第5次再次后台线程无法启动:( – Ryan