你能否告诉我为什么会收到这个错误?我做错了什么?
int[] songPos = {R.raw.position1,R.raw.position2,R.raw.position3,R.raw.position4};
UnitPackage=getApplicationContext().getPackageName();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int resId = getResources().getIdentifier("raw/"+songPos[position], null, UnitPackage);
MediaPlayer mp = MediaPlayer.create(CentralActivity.this, resId);
if (mp.isPlaying()) {
mp.pause();
} else {
mp.start();
}
}
});
07-11 10:06:26.051: W/ResourceType(21457): No package identifier when getting value for resource number 0x00000000
07-11 10:06:26.051: D/AndroidRuntime(21457): Shutting down VM
07-11 10:06:26.051: W/dalvikvm(21457): threadid=1: thread exiting with uncaught exception (group=0x418268b0)
07-11 10:06:26.071: E/AndroidRuntime(21457): FATAL EXCEPTION: main
07-11 10:06:26.071: E/AndroidRuntime(21457): android.content.res.Resources$NotFoundException: Resource ID #0x0
07-11 10:06:26.071: E/AndroidRuntime(21457): at android.content.res.Resources.getValue(Resources.java:1125)
07-11 10:06:26.071: E/AndroidRuntime(21457): at android.content.res.Resources.openRawResourceFd(Resources.java:1083)
07-11 10:06:26.071: E/AndroidRuntime(21457): at android.media.MediaPlayer.create(MediaPlayer.java:837)
07-11 10:06:26.071: E/AndroidRuntime(21457): at goldenRing.rootapp.com.CentralActivity$1.onItemClick(CentralActivity.java:75)
07-11 10:06:26.071: E/AndroidRuntime(21457): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-11 10:06:26.071: E/AndroidRuntime(21457): at android.widget.AbsListView.performItemClick(AbsListView.java:1104)
07-11 10:06:26.071: E/AndroidRuntime(21457): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2792)
07-11 10:06:26.071: E/AndroidRuntime(21457): at android.widget.AbsListView$1.run(AbsListView.java:3468)
07-11 10:06:26.071: E/AndroidRuntime(21457): at android.os.Handler.handleCallback(Handler.java:730)
07-11 10:06:26.071: E/AndroidRuntime(21457): at android.os.Handler.dispatchMessage(Handler.java:92)
07-11 10:06:26.071: E/AndroidRuntime(21457): at android.os.Looper.loop(Looper.java:213)
07-11 10:06:26.071: E/AndroidRuntime(21457): at android.app.ActivityThread.main(ActivityThread.java:5225)
07-11 10:06:26.071: E/AndroidRuntime(21457): at java.lang.reflect.Method.invokeNative(Native Method)
07-11 10:06:26.071: E/AndroidRuntime(21457): at java.lang.reflect.Method.invoke(Method.java:525)
07-11 10:06:26.071: E/AndroidRuntime(21457): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
07-11 10:06:26.071: E/AndroidRuntime(21457): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-11 10:06:26.071: E/AndroidRuntime(21457): at dalvik.system.NativeStart.main(Native Metho
d)
我在这里做错了什么?我错过了什么或?我是否需要为此找到另一种方法或者它可以解决?
答案 0 :(得分:0)
更改
int resId = getResources().getIdentifier("raw/"+songPos[position], null, UnitPackage);
到
int resId = songPos[position];
您已将所有id
存储在songPos
中。无需使用getIdentifier