MediaPlayer.start()对空对象的引用

时间:2019-04-02 00:55:58

标签: java android android-mediaplayer

我一直在创建一个警报应用程序,以运行用户选择的音乐曲目。 因此,在用户选择了轨道之后,可以通过".toString"方法将其从Uri转换为String,以便能够从广播接收器接收该Uri,以便在将其转换回之后可以在MediaPlayer.create();中使用到乌里

Uri.parse(String track);

问题来了,当我尝试播放该曲目时不起作用,尽管当我在另一项活动中对其进行测试时,它仍然可以正常工作,并且我检查了uri.getPath(),它告诉了我音轨的路径,因此问题不存在因为Uri对象为空。

public class AlarmTrigger extends Activity {
    String trackPath;
    Uri trackUri;
    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_trigger);

        trackPath = getIntent().getExtras().getString("Track path"); 
        //getIntent from BroadcastReceiver class
        trackUri = Uri.parse(trackPath);
        Toast.makeText(this, trackUri.getPath(),Toast.LENGTH_SHORT).show(); // it works fine
        AlarmGoes();
    }

    public void AlarmGoes(){
        MediaPlayer mediaPlayer = MediaPlayer.create(this,trackUri); 
        mediaPlayer.start();
    }
}

堆栈跟踪:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amr.mineapps.myapplication/com.amr.mineapps.awarm.AlarmTrigger}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1004)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:983)
at android.media.MediaPlayer.create(MediaPlayer.java:890)
at android.media.MediaPlayer.create(MediaPlayer.java:867)
at android.media.MediaPlayer.create(MediaPlayer.java:846)
at com.amr.mineapps.awarm.AlarmTrigger.onCreate(AlarmTrigger.java:40)
at android.app.Activity.performCreate(Activity.java:6845)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2700)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:165) 
at android.app.ActivityThread.main(ActivityThread.java:6375) 
at java.lang.reflect.Method.invoke(Native Method)

1 个答案:

答案 0 :(得分:0)

尝试此代码

MediaPlayer mediaPlayer = new MediaPlayer();

 // mediaPlayer.setDataSource(String.valueOf(myUri));
  mediaPlayer.setDataSource(AlarmTrigger.this,trackUri);
  mediaPlayer.prepare();   
  mediaPlayer.start();

在上一个活动中,您应在发送数据https://commonsware.com/blog/2016/08/10/uri-access-lifetime-shorter-than-you-might-think.html之前在意图中包含FLAG_GRANT_READ_URI_PERMISSION

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);