AndEngine SoundManager中出现空指针异常
我刚刚编写了从 AndExample 复制的以下几行代码。
try {
this.collision_sound = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "touch.ogg");
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
Log.d("Error","in creation "+e);
}
this.collision_sound.play();
现在的问题是,当我播放声音时,它给了我一个空指针异常。任何人都可以帮助我解决这个问题吗?
I just coded the following lines copied from AndExample.
try {
this.collision_sound = SoundFactory.createSoundFromAsset(this.mEngine.getSoundManager(), this, "touch.ogg");
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
Log.d("Error","in creation "+e);
}
this.collision_sound.play();
Now The problem is that when i play the sound it gives me a null pointer exception. Any one can help me with this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后我明白了问题所在。实际上,上面的代码是正确的,我所缺少的是,当我创建引擎时,我缺少为加载引擎中的声音设置选项。
这解决了我缺少 .setNeedsSound(true) 选项的问题。
Finally I got the point where the problem was. Actually the above code is right what I was missing is that when I was creating the engine I was missing to set it options for sounds in on load engine like.
this solved my problem I was missing the .setNeedsSound(true) option.
你的代码似乎没问题。尝试将createsound函数放在AndEngine的onLoad()方法中,并在其他地方调用play()方法。我想你没有给引擎时间来设置声音变量。
Your code seems to be fine. Try to put the createsound function in the onLoad() method of AndEngine and call the play() method in another place. I suppose you're not giving the time to the engine to set up the sound variable.