OnListItemClick中的空指针

时间:2013-06-17 12:06:27

标签: android listview android-listview android-mediaplayer

所以我使用这段代码来访问我想播放的歌曲数据但是每当我使用媒体播放器时我都会得到空指针异常。我用toast来检查文件名即文件的路径是否正确但它的出现就好了。

protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
    //  super.onListItemClick(l, v, position, id);
    //  l.setClickable(false);
         music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                            musiccursor.moveToPosition(position);
                            String filename = musiccursor.getString(music_column_index);

                      try {
                        mMediaPlayer.setDataSource(filename);
                        mMediaPlayer.prepare();
                        mMediaPlayer.start();
                    } catch (IllegalArgumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (SecurityException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

        Toast.makeText(getApplicationContext(),filename, Toast.LENGTH_LONG).show();

    }

我在这一行收到错误

mMediaPlayer.setDataSource(filename);

logcat的: -

06-17 17:31:36.716: E/AndroidRuntime(5059): FATAL EXCEPTION: main
06-17 17:31:36.716: E/AndroidRuntime(5059): java.lang.NullPointerException
06-17 17:31:36.716: E/AndroidRuntime(5059):     at prac.tise_music.MainActivity.onListItemClick(MainActivity.java:136)
06-17 17:31:36.716: E/AndroidRuntime(5059):     at android.app.ListActivity$2.onItemClick(ListActivity.java:342)
06-17 17:31:36.716: E/AndroidRuntime(5059):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
06-17 17:31:36.716: E/AndroidRuntime(5059):     at android.widget.ListView.performItemClick(ListView.java:3569)
06-17 17:31:36.716: E/AndroidRuntime(5059):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1831)
06-17 17:31:36.716: E/AndroidRuntime(5059):     at android.os.Handler.handleCallback(Handler.java:587)
06-17 17:31:36.716: E/AndroidRuntime(5059):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-17 17:31:36.716: E/AndroidRuntime(5059):     at android.os.Looper.loop(Looper.java:150)
06-17 17:31:36.716: E/AndroidRuntime(5059):     at android.app.ActivityThread.main(ActivityThread.java:4389)
06-17 17:31:36.716: E/AndroidRuntime(5059):     at java.lang.reflect.Method.invokeNative(Native Method)
06-17 17:31:36.716: E/AndroidRuntime(5059):     at java.lang.reflect.Method.invoke(Method.java:507)
06-17 17:31:36.716: E/AndroidRuntime(5059):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
06-17 17:31:36.716: E/AndroidRuntime(5059):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
06-17 17:31:36.716: E/AndroidRuntime(5059):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

如果您正在从外部存储中读取文件,那么首先在您的表现中提供“读取外部存储权限”。因为你无法读取文件。

答案 1 :(得分:0)

讨论了类似的问题here

可能符合您的目的。

还有一个建议,请尝试MediaPlayer.create(),您也可能只想在玩家实际准备就绪后开始,例如this