从列表视图播放音乐

时间:2014-01-11 18:31:02

标签: android listview media-player

您好我有列表,我有本地音乐文件。当我点击列表项时,我想播放我的音乐文件。 继承我的onclick列表项看起来:

lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            switch( position )
            {
               case 0:  
                   v.contentEquals("file:///android_asset/disco.mp3");
                   playSound(v);
               case 1:
                  v.contentEquals("file:///android_asset/disco.mp3");
                   playSound(v); 
                       break;
            };

        }

并且我的媒体播放器看起来像是:

private void playSound(String v) {
            // TODO Auto-generated method stub
            try {
                mp.setDataSource(v);
            } 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();
            }
            mp.start();
        }

当我点击列表项目程序冻结并强行关闭。 我该怎么办?

logcatoutput

  

01-11 23:19:19.191:E / AndroidRuntime(4811):致命异常:主要   01-11 23:19:19.191:E / AndroidRuntime(4811):java.lang.NullPointerException   01-11 23:19:19.191:E / AndroidRuntime(4811):at com.hmomeni.customlistview.MainActivity $ 11.onItemClick(MainActivity.java:113)   01-11 23:19:19.191:E / AndroidRuntime(4811):在android.widget.AdapterView.performItemClick(AdapterView.java:301)   01-11 23:19:19.191:E / AndroidRuntime(4811):在android.widget.AbsListView.performItemClick(AbsListView.java:1519)   01-11 23:19:19.191:E / AndroidRuntime(4811):在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3291)   01-11 23:19:19.191:E / AndroidRuntime(4811):在android.widget.AbsListView $ 1.run(AbsListView.java:4340)   01-11 23:19:19.191:E / AndroidRuntime(4811):在android.os.Handler.handleCallback(Handler.java:725)   01-11 23:19:19.191:E / AndroidRuntime(4811):在android.os.Handler.dispatchMessage(Handler.java:92)   01-11 23:19:19.191:E / AndroidRuntime(4811):在android.os.Looper.loop(Looper.java:137)   01-11 23:19:19.191:E / AndroidRuntime(4811):在android.app.ActivityThread.main(ActivityThread.java:5328)   01-11 23:19:19.191:E / AndroidRuntime(4811):at java.lang.reflect.Method.invokeNative(Native Method)   01-11 23:19:19.191:E / AndroidRuntime(4811):at java.lang.reflect.Method.invoke(Method.java:511)   01-11 23:19:19.191:E / AndroidRuntime(4811):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)   01-11 23:19:19.191:E / AndroidRuntime(4811):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)   01-11 23:19:19.191:E / AndroidRuntime(4811):at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

“v”是普通字符串?如果是这样,因为它是不可变的,你不能通过一个函数改变它:

v.contentEquals("file:///android_asset/disco.mp3");

你应该使用普通的“=”来设置它:

v="file:///android_asset/disco.mp3";