点击列表项时获取NPE

时间:2014-11-07 05:54:35

标签: android android-listview android-mediaplayer

我在列表视图中列出音频文件并播放一个用户点击的音频文件,但是当用户点击列表中的任何其他歌曲时,如果已经播放了第一首歌曲,则获取NPE ...

   mediaPlayer = new MediaPlayer();

   .............................

   listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long id) {                  

                strNURL = videoMovieArrayList.get(position).getUrl().toString();                

                if(mediaPlayer!=null && mediaPlayer.isPlaying())
                    try {
                        mediaPlayer.stop();
                        mediaPlayer.release(); 
                      } catch (Exception e) { 

                      }
                    play(arg1);
                }
        }); 


  public void play(View view){         
       mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); // NPE
        try {
            mediaPlayer.setDataSource(strNURL);
        } catch (IllegalArgumentException e) {
        ............
    });
   }

请查看下面的日志,这是我在点击任何其他列表项时所获得的

logcat的: -

11-07 11:29:03.430: E/MessageQueue-JNI(6092): java.lang.NullPointerException
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at com.exa.info.MusicTrackActivity.play(MusicTrackActivity.java:240)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at com.exa.info.MusicTrackActivity$3.onItemClick(MusicTrackActivity.java:187)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.widget.AdapterView.performItemClick(AdapterView.java:299)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.widget.AbsListView.onTouchUp(AbsListView.java:3650)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.widget.AbsListView.onTouchEvent(AbsListView.java:3429)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.View.dispatchTouchEvent(View.java:7706)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2210)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1945)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2071)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1518)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchTouchEvent(ActionBarActivityDelegateICS.java:268)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2019)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.View.dispatchPointerEvent(View.java:7886)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.os.MessageQueue.nativePollOnce(Native Method)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.os.MessageQueue.next(MessageQueue.java:138)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.os.Looper.loop(Looper.java:131)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at android.app.ActivityThread.main(ActivityThread.java:5061)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at java.lang.reflect.Method.invokeNative(Native Method)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at java.lang.reflect.Method.invoke(Method.java:515)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:603)
11-07 11:29:03.430: E/MessageQueue-JNI(6092):   at dalvik.system.NativeStart.main(Native Method)
11-07 11:29:03.430: D/AndroidRuntime(6092): Shutting down VM

2 个答案:

答案 0 :(得分:2)

在try-catch中,您正在制作mediaPlayer = null;,然后调用play方法,您尝试使用NULL值设置setAudioStreamType。

所以NPE发生了。

从try-catch中删除mediaPlayer = null;

在release()之后,该对象不再可用。

所以,创建mediaPlayer = new MediaPlayer();,然后调用play方法。

        mediaPlayer.setDataSource(strNURL));
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
        mediaPlayer.prepare();
        mediaPlayer.start();

答案 1 :(得分:0)

问题出在这里,您可以释放mediaPlayer对象

if(mediaPlayer!=null && mediaPlayer.isPlaying())
                try {
                    mediaPlayer.stop();
                    mediaPlayer.release(); 
                  } catch (Exception e) { 

                  }
                play(arg1);
            }

并将方法play(arg1);调用到下一个,但此时您的mediaPlayer对象将为null,因此您将获得NPE。所以只需使用此

  listview.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position,
            long id) {                  

            strNURL = videoMovieArrayList.get(position).getUrl().toString();                

            if(mediaPlayer!=null)
                try {
                    play(arg1);
                  } catch (Exception e) { 

                  }

            }
    });