0

All my songs are playing but there are a few songs so that when I click, the app closes what is wrong ? My Extarnal uri is parfect but some sone crash my musik player how to solve this.In the case of just a few songs, this problem can be seen now

    Here's my code:

    2022-03-08 19:07:50.236 4014-4014/com.codewithmorshed.musikc E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.codewithmorshed.musikc, PID: 4014
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.codewithmorshed.musikc/com.codewithmorshed.musikc.PlayerActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3430)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2199)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
        at com.codewithmorshed.musikc.PlayerActivity.getIntentmethod(PlayerActivity.java:389)
        at com.codewithmorshed.musikc.PlayerActivity.onCreate(PlayerActivity.java:56)
        at android.app.Activity.performCreate(Activity.java:7458)


 private void getIntentmethod() {
    position = (int) getIntent().getSerializableExtra("position");
    songlists = songs;

    if (songlists != null) {
        play.setImageResource(R.drawable.pausee);

        uri = Uri.parse(songlists.get(position).getPath());
    }
    if (mediaPlayer != null) {
        mediaPlayer.start();
        mediaPlayer.reset();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
        mediaPlayer.start();
    } else {
        mediaPlayer = new MediaPlayer();
        mediaPlayer = MediaPlayer.create(getApplicationContext(), uri);
        try {
            try {
                mediaPlayer.setDataSource(context,uri);
            } catch (Exception e) {
                mediaPlayer.setDataSource(context, uri);
            }
            mediaPlayer.prepare(); // prepare it, after you can change volume ...
        } catch (Exception e){
            e.printStackTrace();
        }
        mediaPlayer.start(); // finally start the med
    }

   seekBar.setMax(mediaPlayer.getDuration() / 1000);
    metaData(uri);
}
Morshed
  • 19
  • 1

0 Answers0