简体   繁体   English

在两个活动中加载“原始” MediaPlayer资源时出现空点异常

[英]Null point exception when loading a 'raw' MediaPlayer resource in two activities

I am struggling with loading a sound in my application. 我正在努力在应用程序中加载声音。 I'm receiving a null point exception when trying to create the same "sound" in two activities. 尝试在两个活动中创建相同的“声音”时,我收到零点异常。 I have a variable clickSound that i declare in both activities which accesses the same Raw file. 我有一个变量clickSound,我在两个访问相同Raw文件的活动中声明。 I have added a folder named 'raw' in the 'res' folder and the file clicksound.mp3 in it. 我在“ res”文件夹中添加了一个名为“ raw”的文件夹,并在其中添加了clicksound.mp3文件。

The thing is, for MainActivity is working perfect. 问题是,对于MainActivity来说是完美的。 for Second activity i just receive the exception. 对于第二个活动,我只是收到例外。

Main Activity: 主要活动:

// Declare clickSound onCreate
        final MediaPlayer clickSound = MediaPlayer.create(this, R.raw.click_button);

Second Activity: 第二项活动:

private final MediaPlayer mClickSound = MediaPlayer.create(this, R.raw.click_button);

The strange thing is that it worked some time ago but now i don't understand why not anymore. 奇怪的是,它已经有一段时间了,但现在我不明白为什么不再这样了。 Any suggestions what is happening? 有什么建议吗?

This line: 这一行:

private final MediaPlayer mClickSound = MediaPlayer.create(this, R.raw.click_button);`

for that moment the instace in not valid initialized, so this is not givin all that you need to create a media player.... move that to the onCreate method 在那一刻,无效初始化的实例,所以不是你创建媒体播放器所需的全部内容....将其移动到onCreate方法

I think this error is related when you don't initialize your class. 我认为与您不初始化类有关的此错误。

Try before something like this: 请尝试以下操作:

MediaPlayer mClickSound = new MediaPlayer();

Or full code: 或完整代码:

private final MediaPlayer mClickSound;
//other things
MediaPlayer mClickSound = new MediaPlayer();
mClickSound = MediaPlayer.create(this, R.raw.click_button);

It could be also that you're using two different names "clickSound" and "mClickSound" 也可能是您使用了两个不同的名称“ clickSound”和“ mClickSound”

The problem was not with the MediaPlayer file. 问题不在于MediaPlayer文件。 I had an error with my Json Reader that was not handling well an exception. 我的Json Reader出错,但是处理不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM