0

I am a total beginner in programing and this is my first program. I lost countless hours to try to make it work but nothing works. Everytime I click on the button to open new activity the program crashes and I don't know why, the build finishes and everything is ok.

Here is the activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:padding="16dp"
   >


    <FrameLayout
        android:layout_width="395dp"
        android:layout_height="298dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/image" />
    </FrameLayout>

    <LinearLayout
        android:layout_width="395dp"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:orientation="horizontal"
        android:padding="16dp">

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="3sp"
            android:layout_weight="1"
            android:onClick="music"
            android:text="Play" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="3sp"
            android:layout_weight="1"
            android:onClick="music"
            android:text="Pause" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="3sp"
            android:layout_weight="1"
            android:onClick="music"
            android:text="Stop" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="32dp"
        android:layout_marginBottom="19dp"
        android:gravity="bottom"
        android:orientation="horizontal"
        android:layout_centerVertical="true">

        <Button
            android:id="@+id/button20"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="12sp"
            android:layout_marginRight="12sp"
            android:layout_weight="1"
            android:text="test1" />

        <Button
            android:id="@+id/button21"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="12sp"
            android:layout_marginRight="12sp"
            android:layout_weight="1"
            android:text="test2" />
    </LinearLayout>

</RelativeLayout>``

And here is the mainactivity.java

package com.example.app1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button button20;
    Button button21;

    MediaPlayer mediaPlayer;

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

        button20 = findViewById(R.id.button20);
        button20.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), About.class);
                startActivity(intent);
            }
        });


        button21 = findViewById(R.id.button21);
        button21.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getApplicationContext(), About.class);
                    startActivity(intent);
                }
            });

        mediaPlayer = null;




        };





    public void music(View view) {
        switch (view.getId()){
            case R.id.button:
                if (mediaPlayer == null){
                    mediaPlayer = MediaPlayer.create(this, R.raw.music);
                }
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        stopMusic();
                    }
                });
                mediaPlayer.start();
                break;
            case R.id.button2:
                if(mediaPlayer != null)
                    mediaPlayer.pause();
                break;
            case R.id.button3:
                if(mediaPlayer !=null){
                    mediaPlayer.stop();
                    stopMusic();
                }
                break;

        }
    }

    private void stopMusic() {
        mediaPlayer.release();
        mediaPlayer = null;
    }


    @Override
    protected void onStop() {
        super.onStop();
        stopMusic();
    }



}

The app started crashing after I added this line of code:

 button20 = findViewById(R.id.button20);
        button20.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), About.class);
                startActivity(intent);
            }
        });


        button21 = findViewById(R.id.button21);
        button21.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getApplicationContext(), About.class);
                    startActivity(intent);
                }
            });

Here is the stacktrace:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.prog1, PID: 5698
    java.lang.RuntimeException: Unable to stop activity {com.example.prog1/com.example.prog1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.release()' on a null object reference
        at android.app.ActivityThread.callActivityOnStop(ActivityThread.java:4858)
        at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:4829)
        at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4903)
        at android.app.servertransaction.StopActivityItem.execute(StopActivityItem.java:40)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.release()' on a null object reference
        at com.example.prog1.MainActivity.stopMusic(MainActivity.java:84)
        at com.example.prog1.MainActivity.onStop(MainActivity.java:92)
        at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1476)
        at android.app.Activity.performStop(Activity.java:8215)
        at android.app.ActivityThread.callActivityOnStop(ActivityThread.java:4850)
        at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:4829) 
        at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4903) 
        at android.app.servertransaction.StopActivityItem.execute(StopActivityItem.java:40) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
I/Process: Sending signal. PID: 5698 SIG: 9
Jozef
  • 3
  • 2

1 Answers1

0

You have already checked the mediaPlayer if it is not null a few times but I think you should check it also after calling MediaPlayer.release() when your activity stopd

private void stopMusic() {
    if(mediaPlayer!=null){
        mediaPlayer.release();
     }
}
Shay Kin
  • 2,539
  • 3
  • 15
  • 22
  • Thanks for answering Shay, yes you were right, after I changed the code from private void stopMusic() { mediaPlayer.release(); mediaPlayer = null; } to private void stopMusic() { mediaPlayer.release(); mediaPlayer = null; It works and I can go normaly to other activities, but now my music player isnt working properly, when I click the button Play and then stop the app crashes after this code – Jozef Mar 12 '21 at 22:15
  • OK i figured it out that I must writhe the code like this: private void stopMusic() { if(mediaPlayer!=null){ mediaPlayer.release(); mediaPlayer = null ; } } Thank you all for helping, I learned a lot today – Jozef Mar 12 '21 at 22:27
  • Hi, I see you're new to SO. If you feel an answer solved the problem, please mark it as 'accepted' by clicking the green check mark. This helps keep the focus on older SO which still don't have answers. – Shay Kin Mar 12 '21 at 22:50