I've been working on creating an alarm app to run a music track that the user selects.
So, after the user selects the track, it is converted from Uri to String by ".toString"
method to be able to receive this Uri from a broadcast receiver so that it can be used in MediaPlayer.create();
after converting it back to Uri
Uri.parse(String track);
The problem comes when i try to play this it doesn't work although when I tested this in another activity it worked fine and I checked the uri.getPath()
it tells me the path of the track so the problem is not because Uri object is empty.
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();
}
}
The stack-trace:
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)