[英]MediaPlayer, onItemClickListener and a NullPointerException. What's wrong?
I'm working on an Android soundboard at the moment and so far it's going fine. 我目前正在开发Android音板,到目前为止一切正常。 I've made a beautiful listView with images etc., but i just can't get the sounds to load properly.
我用图像等制作了漂亮的listView,但我只是无法正确加载声音。 The locations of the soundfiles are stored within the arraylist and I'm trying to load them with getById().sound;
声音文件的位置存储在arraylist中,我正尝试使用getById()。sound加载它们。
Here's what I've done 这就是我所做的
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mp = MediaPlayer.create(MainActivity.this, Uri.parse("android.resource://" + getPackageName() + "/" +
Model.GetbyId(position + 1).Sound));
if(mp == null) {
Toast.makeText(getApplicationContext(),
"Mediaplayer is null. Link was: "+link , Toast.LENGTH_LONG)
.show();
} else {
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaplayer) {
mediaplayer.stop();
mediaplayer.release();
}
});
mp.start();
}
}
});
Can anyone tell my why it doesn't work? 谁能告诉我为什么它不起作用? I'm still a beginner and I couldn't figure it out after hours of trying and researching.
我仍然是一个初学者,经过数小时的尝试和研究,我无法弄清楚。
EDIT: Here's the error in the logcat. 编辑:这是logcat中的错误。 I'm pretty sure the file is there...
我很确定文件在那里...
07-28 12:56:53.998 124-593/? I/AwesomePlayer: setDataSource_l(URL suppressed)
07-28 12:56:53.998 124-7235/? E/: Failed to open file 'R.raw.so_schoenasummervielheu'. (No such file or directory)
07-28 12:56:53.998 6760-6771/? E/MediaPlayer: error (1, -2147483648)
07-28 12:56:54.008 6760-6760/? D/MediaPlayer: create failed:
java.io.IOException: Prepare failed.: status=0x1
at android.media.MediaPlayer.prepare(Native Method)
at android.media.MediaPlayer.create(MediaPlayer.java:792)
at android.media.MediaPlayer.create(MediaPlayer.java:769)
at com.spruechmeischtr2000.android.MainActivity$1.onItemClick(MainActivity.java:45)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
at android.widget.AbsListView$1.run(AbsListView.java:3463)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
EDIT: Just want to answer my own question here (StackOverflow won't let me do so for three more hours). 编辑:只想在这里回答我自己的问题(StackOverflow不会让我再等三个小时)。 Actually the code was fine, the one thing i did wrong was that i declared my soundfiles as string variables instead of int resource ids in my ArrayList/Itemadapter.
实际的代码很好,我做错的一件事是我将声音文件声明为字符串变量,而不是ArrayList / Itemadapter中的int资源ID。 Thanks to all the people who tried to help me :)
感谢所有试图帮助我的人:)
不要忘记添加mediaplayer.prepare();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.