设置从资产文件夹中的文件导致的MediaPlayer到空指针异常 [英] Setting a file from asset folder causes MediaPlayer to nullpointer exception
问题描述
我的资产文件夹中的文件列表,名为文件清单:。我告诉所有在ListView元素,所以他们中的一些已经被点击时,我希望它播放。在这里,它给我的 NullPointerException异常。 code,会导致一个错误:
I have a list of asset folder files, named filelist. I show all that elements in the ListView, so when some of them has been clicked i want it to be played. And here it gives me nullpointerexception. Code, that causes an error:
try {
selectedItemPlayer.setDataSource(
aMan.openFd(filelist[selectedItem]).getFileDescriptor(),
aMan.openFd(filelist[selectedItem]).getStartOffset(),
aMan.openFd(filelist[selectedItem]).getLength());
selectedItemPlayer.prepare();
selectedItemPlayer.start();
selectedItemPlayer.setLooping(false);
} catch (IllegalArgumentException e) {
...
...
祖阿曼是 assetManager 的文件清单:是具有资产目录中的文件列表中的将selectedItem 是元素在列表和我想打了clicled。所以,当我点击它的NullPointerException崩溃列表项。看起来,听起来不加载到媒体播放器。奇怪,因为我使用文件清单:,用的Soundpool和正常工作...任何想法?
aMan is assetManager, filelist is the list of asset directory files, selectedItem is the element that has been clicled at the list and which i want to play. SO when I click to the list-item it crashes with nullPointerException. Seems, sounds not loading to mediaplayer. Strange, because I use filelist, with soundPool and it works fine... ANy idea?
UPD1:logcat的消息
UPD1: Logcat message
12-09 14:17:06.964: W/dalvikvm(23404): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
12-09 14:17:06.974: E/AndroidRuntime(23404): FATAL EXCEPTION: main
12-09 14:17:06.974: E/AndroidRuntime(23404): java.lang.NullPointerException
12-09 14:17:06.974: E/AndroidRuntime(23404): at uk.co.futurelite.elite.drum.machine.MainActivity$1.onClick(MainActivity.java:251)
12-09 14:17:06.974: E/AndroidRuntime(23404): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:924)
12-09 14:17:06.974: E/AndroidRuntime(23404): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
12-09 14:17:06.974: E/AndroidRuntime(23404): at android.widget.AbsListView.performItemClick(AbsListView.java:1065)
12-09 14:17:06.974: E/AndroidRuntime(23404): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2522)
12-09 14:17:06.974: E/AndroidRuntime(23404): at android.widget.AbsListView$1.run(AbsListView.java:3183)
12-09 14:17:06.974: E/AndroidRuntime(23404): at android.os.Handler.handleCallback(Handler.java:605)
12-09 14:17:06.974: E/AndroidRuntime(23404): at android.os.Handler.dispatchMessage(Handler.java:92)
12-09 14:17:06.974: E/AndroidRuntime(23404): at android.os.Looper.loop(Looper.java:137)
12-09 14:17:06.974: E/AndroidRuntime(23404): at android.app.ActivityThread.main(ActivityThread.java:4441)
12-09 14:17:06.974: E/AndroidRuntime(23404): at java.lang.reflect.Method.invokeNative(Native Method)
12-09 14:17:06.974: E/AndroidRuntime(23404): at java.lang.reflect.Method.invoke(Method.java:511)
12-09 14:17:06.974: E/AndroidRuntime(23404): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-09 14:17:06.974: E/AndroidRuntime(23404): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-09 14:17:06.974: E/AndroidRuntime(23404): at dalvik.system.NativeStart.main(Native Method)
UPD2:
在行251我有以下code
UPD2: At line 251 i have the following code
selectedItemPlayer.setDataSource(aMan.openFd(filelist[selectedItem]).getFileDescriptor(),
文件清单[selectedItem设置] 不能为空,因为我只是印它的logcat
filelist[selectedItem] cant be null, because i just printed it in logcat
12-09 14:31:58.442: V/TESTing filelist(1819): dk9_ch.WAV
不知道...
UPD3:现在的作品,感谢Luksprog,我应该初始化。不知道。
UPD3: now it works, thanks to Luksprog , I was supposed to initialize it. Didnt know.
推荐答案
将您的code如下,以帮助跟踪您的问题*
Break your code as below to help track your problem*
AssetFileDescriptor afd = getAssets().openFd(filelist[selectedItem]);
selectedItemPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
似乎文件清单[selectedItem设置]
返回null或它不存在于你的资产foldre。检查您的主要活动在行251
It seems that filelist[selectedItem]
returns null or it doesnt exist in your assets foldre. Check your main activity at line 251
这篇关于设置从资产文件夹中的文件导致的MediaPlayer到空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!