带有Uri的setDataSource导致NullPointerException

时间:2016-08-22 00:47:49

标签: java android nullpointerexception uri media-player

嗯,这是onClick的{​​{1}} method

button

在我处理输出之外:

public void aggiungi (View v){

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("audio/*");
    startActivityForResult(Intent.createChooser(intent, "Select"), PICK_AUDIO_REQUEST_CODE);

}

这里是名为

的setAudioToButton @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PICK_AUDIO_REQUEST_CODE){ if (resultCode == RESULT_OK){ Uri uri_to_use = data.getData(); setAudioToButton(uri_to_use); } } }
method

问题是,当我点击新按钮使其发出声音时,我有public void setAudioToButton(final Uri audio){ PercentRelativeLayout percentRelativeLayout = (PercentRelativeLayout)findViewById(R.id.main_layout); PercentRelativeLayout.LayoutParams layoutParams = new PercentRelativeLayout.LayoutParams(43, 12); layoutParams.addRule(PercentRelativeLayout.ALIGN_PARENT_RIGHT); layoutParams.addRule(PercentRelativeLayout.BELOW, R.id.quattro); Button button = new Button(this); button.setLayoutParams(layoutParams); PercentRelativeLayout.LayoutParams layoutParams1 = (PercentRelativeLayout.LayoutParams)button.getLayoutParams(); PercentLayoutHelper.PercentLayoutInfo percentLayoutInfo = layoutParams1.getPercentLayoutInfo(); percentLayoutInfo.heightPercent = 0.12f; percentLayoutInfo.widthPercent = 0.43f; percentLayoutInfo.topMarginPercent = 0.063f; percentLayoutInfo.leftMarginPercent = 0.007f; button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (mediaPlayer != null){mediaPlayer.stop();} try {mediaPlayer.setDataSource(getBaseContext(), audio);} catch (IOException error){error.printStackTrace();} try { mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.start(); } }); button.setText("start"); percentRelativeLayout.addView(button); }

NullPointerException

我想知道为什么会发生这种情况,我无法理解

**

编辑:

**

我已将其放入08-22 02:32:43.164 11880-11880/com.example.utente.usefulsounds E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.utente.usefulsounds, PID: 11880 Theme: themes:{} java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setDataSource(android.content.Context, android.net.Uri)' on a null object reference at com.example.utente.usefulsounds.MainActivity$1.onClick(MainActivity.java:182) at android.view.View.performClick(View.java:5204) at android.view.View$PerformClick.run(View.java:21158) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5461) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) onCreate

method

但它没有效果

现在我以不同的方式尝试过:将if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE_REQUEST_CODE); } 传递给Uri,我已经通过了MediaPlayer

filedescriptor

按钮出现时带有文字"选择",因此 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (mediaPlayer != null){mediaPlayer.stop();} File file = new File(audio.getPath()); FileInputStream fileInputStream; try { fileInputStream = new FileInputStream(file); try {mediaPlayer.setDataSource(fileInputStream.getFD());} catch (IOException error){error.printStackTrace();} }catch (IOException ioe){button.setText("FILE ERROR");} try { mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.start(); } }); 没有任何问题(否则它应该是"文件错误") ,但是当我点击它时,file导致NullPointerException导致崩溃。为什么这个?我无法理解

1 个答案:

答案 0 :(得分:0)

在这里回答

您不必使用setDataSource() method,而只需使用create() method:)

例如

 MediaPlayer.create(getApplicationContext(), yourAudioUri).start();

快乐的编码,玩得开心:)