Setting a file from asset folder causes MediaPlayer to nullpointer exception

故事扮演 提交于 2019-12-11 16:15:23

问题


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) {
   ...
   ...

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 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: At line 251 i have the following code

    selectedItemPlayer.setDataSource(aMan.openFd(filelist[selectedItem]).getFileDescriptor(),

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

Dont know...

UPD3: now it works, thanks to Luksprog , I was supposed to initialize it. Didnt know.


回答1:


Break your code as below to help track your problem*

AssetFileDescriptor afd = getAssets().openFd(filelist[selectedItem]);
selectedItemPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());

It seems that filelist[selectedItem] returns null or it doesnt exist in your assets foldre. Check your main activity at line 251



来源:https://stackoverflow.com/questions/13786099/setting-a-file-from-asset-folder-causes-mediaplayer-to-nullpointer-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!