我有一个名为filelist的资产文件夹文件列表。我在 ListView 中显示所有元素,因此当单击其中一些元素时,我希望播放它。在这里它给了我nullpointerexception。导致错误的代码:
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) {
...
...
aMan是assetsManager,filelist是资产目录文件的列表,selectedItem是已在列表中单击并且我要播放的元素。因此,当我单击列表项时,它会因 nullPointerException 而崩溃。似乎,声音没有加载到媒体播放器。奇怪,因为我使用filelist和 soundPool 并且它工作正常......知道吗?
UPD1:Logcat 消息
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 行,我有以下代码
selectedItemPlayer.setDataSource(aMan.openFd(filelist[selectedItem]).getFileDescriptor(),
filelist[selectedItem]不能为空,因为我只是在 logcat 中打印了它
12-09 14:31:58.442: V/TESTing filelist(1819): dk9_ch.WAV
不知道...
UPD3:现在它可以工作了,感谢 Luksprog ,我应该初始化它。不知道。