Nullpointer Exception“void android.media.MediaPlayer.setDataSource(android.content.Context,android.net.Uri)”

时间:2016-03-14 06:18:13

标签: android android-fragments android-intent nullpointerexception mediastore

我正在尝试从片段中的用户的音乐文件夹中获取音乐文件,但是当我这样做并在onActivityResult中接收它(并记录它)之后,我得到了一个当我想用媒体播放器播放时,Nullpointer异常。 选择文件的代码:

    Button pickFile = (Button) v.findViewById(R.id.btnpick);
    pickFile.setOnClickListener(new OnClickListener() {

   @Override
        public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 1);
        }
    });

OnActivityResult(不要忘记我在片段中)

  public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data); 
  getActivity();
  if ((resultCode == getActivity().RESULT_OK) && (data != null)) {
  Log.d("result", "okreceived");
  Uri soundUri = Uri.parse(data.getData().toString());
  Log.i("result", "Intent data " + data.getData().toString());
  mplayergo(soundUri);
       }
  if (resultCode == getActivity().RESULT_CANCELED)
  {
  //null
  }
        }

       public void mplayergo(Uri soundUri){
           try {
               mplayer.setDataSource(getActivity(), soundUri);
               mplayer.start();
           } catch (IOException e) {
               e.printStackTrace();

           } catch (Exception e) {
               e.printStackTrace();
           }

        }

这是日志:

  

D /结果:okreceived I / result:Intent数据   content:// media / external / audio / media / 34839 W / System.err:   java.lang.NullPointerException:尝试调用虚方法'void   android.media.MediaPlayer.setDataSource(android.content.Context,   android.net.Uri)'在空对象引用上   com.package.example.GameFragment.mplayergo(GameFragment.java:764)at at   com.package.example.GameFragment.onActivityResult(GameFragment.java:748)   W / System.err:at   android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:156)   W / System.err:at   android.app.Activity.dispatchActivityResult(Activity.java:6290)   W / System.err:at   android.app.ActivityThread.deliverResults(ActivityThread.java:3606)   W / System.err:at   android.app.ActivityThread.handleSendResult(ActivityThread.java:3653)   W / System.err:at   android.app.ActivityThread.access $ 1400(ActivityThread.java:154)   W / System.err:at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1371)   W / System.err:at   android.os.Handler.dispatchMessage(Handler.java:102)W / System.err:
  在android.os.Looper.loop(Looper.java:135)W / System.err:at   android.app.ActivityThread.main(ActivityThread.java:5300)   W / System.err:at java.lang.reflect.Method.invoke(Native Method)   W / System.err:at java.lang.reflect.Method.invoke(Method.java:372)   W / System.err:at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:904)   W / System.err:at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

通过重新初始化MediaPlayer并将URI设置为源来解决。

 public void mplayergo(Uri soundUri){
       try {
           mplayer.stop();
           mplayer.reset();
           mplayer.release();
           mplayer = MediaPlayer.create(getActivity(), soundUri);
          // mplayer.start();
       } catch (Exception e) {
           e.printStackTrace();
       }
    }

1 个答案:

答案 0 :(得分:6)

mplayer未初始化。因此,当您尝试调用方法setDataSource时,它会获得空指针异常。尝试在调用该方法之前初始化mplayer