Squishy47 Squishy47 - 1 month ago 12
Java Question

Android MediaPlayer in it's own class causing app crash

I have created a class for the MediaPlayer so that i can access it from multiple classes as i need to start media in once activity and stop it in a different one.
when trying to call the mediaPlayer i get an error.
I don't understand where i am going wrong with this as i don't fully understand the error.
Before creating the class for the MediaPlayer it was working but it didn't allow me to do what I wanted.
Could someone advise me how to solve my problem or a different way of accessing the MediaPlayer. Thanks in advance.

The MainActivity class is where i am calling the media player from.

public class MainActivity extends AppCompatActivity {
Music gm = new Music();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Display the Main Activity.
setContentView(R.layout.activity_main);

gm.menuSound(true);


}



The MediaPlayer class is where the media player is created and where the mediaPlayer is started and stopped.

public class Music extends gameActivity{
Context context;

MediaPlayer menu;
MediaPlayer soundtrack;
MediaPlayer death;
MediaPlayer start;

public void menuSound(boolean x) {
if(menu == null) {
menu = MediaPlayer.create(context, R.raw.menumusic);
menu.setVolume(100, 100);
}

if(x)
menu.start();
if(!x)
menu.stop();


}



And the error

--------- beginning of crash


E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sam.myfirstapp, PID: 2395
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sam.myfirstapp/com.example.sam.myfirstapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.media.MediaPlayer.create(MediaPlayer.java:931)
at android.media.MediaPlayer.create(MediaPlayer.java:914)
at com.example.sam.myfirstapp.Music.menuSound(Music.java:21)
at com.example.sam.myfirstapp.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

Answer

The error is with null context you have not initialised context in music class.

public class MainActivity extends AppCompatActivity { 
Music gm ;

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    // Display the Main Activity. 
    setContentView(R.layout.activity_main); 
      gm = new Music(this); // pass context here to music  
    gm.menuSound(true); 
}

and Music class like this:

public class Music extends gameActivity{ 
Context context;

MediaPlayer menu;
MediaPlayer soundtrack;
MediaPlayer death;
MediaPlayer start;

 public Music(Context context){
       this.context =context; // initialising context here
      }

public void menuSound(boolean x) {
    if(menu == null) { 
        menu = MediaPlayer.create(context, R.raw.menumusic); // here context was causing null pointer exception as context was null
        menu.setVolume(100, 100); 
    } 

    if(x) 
        menu.start(); 
    if(!x) 
        menu.stop(); 
}