Question

I've created a class called MusicPlayer.

public class MusicPlayer {
    public static MediaPlayer player;
    public static void SoundPlayer(Context ctx,int raw_id){
        player = MediaPlayer.create(ctx, R.raw.music);

         player.setLooping(false); // Set looping
         player.setVolume(100, 100);

         //player.release();
         player.start();
    }
}

But my app keeps crashing when I try to acces it from my activity.

I just call it like this, inside the onCreate:

MusicPlayer.player.start();

I have the log cat error, and I can see that it complains about the; MusicPlayer.player.start(); in my activity. But I can't seem to understand the error.

03-04 07:45:57.608: E/AndroidRuntime(1745): FATAL EXCEPTION: main
03-04 07:45:57.608: E/AndroidRuntime(1745): Process: com.comrades.rocketeer, PID: 1745
03-04 07:45:57.608: E/AndroidRuntime(1745): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.comrades.rocketeer/com.comrades.rocketeer.LaunchScreen}: java.lang.NullPointerException
03-04 07:45:57.608: E/AndroidRuntime(1745):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
03-04 07:45:57.608: E/AndroidRuntime(1745):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-04 07:45:57.608: E/AndroidRuntime(1745):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-04 07:45:57.608: E/AndroidRuntime(1745):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-04 07:45:57.608: E/AndroidRuntime(1745):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-04 07:45:57.608: E/AndroidRuntime(1745):     at android.os.Looper.loop(Looper.java:136)
03-04 07:45:57.608: E/AndroidRuntime(1745):     at android.app.ActivityThread.main(ActivityThread.java:5017)
03-04 07:45:57.608: E/AndroidRuntime(1745):     at java.lang.reflect.Method.invokeNative(Native Method)
03-04 07:45:57.608: E/AndroidRuntime(1745):     at java.lang.reflect.Method.invoke(Method.java:515)
03-04 07:45:57.608: E/AndroidRuntime(1745):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-04 07:45:57.608: E/AndroidRuntime(1745):     at dalvik.system.NativeStart.main(Native Method)
03-04 07:45:57.608: E/AndroidRuntime(1745): Caused by: java.lang.NullPointerException
03-04 07:45:57.608: E/AndroidRuntime(1745):     at com.comrades.rocketeer.LaunchScreen.onCreate(LaunchScreen.java:16)
03-04 07:45:57.608: E/AndroidRuntime(1745):     at android.app.Activity.performCreate(Activity.java:5231)
03-04 07:45:57.608: E/AndroidRuntime(1745):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-04 07:45:57.608: E/AndroidRuntime(1745):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-04 07:45:57.608: E/AndroidRuntime(1745):     ... 11 more

I hope that you can help me out. And spot, what it is that I'm doing wrong.

LaunchActivity per request:

package com.comrades.rocketeer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.comrades.rocketeer.R;

public class LaunchScreen extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        MusicPlayer.player.start(); //line 16

        Thread logoTimer = new Thread() {
            public void run(){
                try{
                    int logoTimer = 0;
                    while(logoTimer < 4000){
                        sleep(100);
                        logoTimer = logoTimer +100;
                    };
                    startActivity(new Intent("com.plambech.CLEARSCREEN"));
                } 

                catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                finally{
                    finish();
                }
            }
        };

        logoTimer.start();
    }
}
Was it helpful?

Solution

You must initialized MusicPlayer class before used like:

MusicPlayer.SoundPlayer(this,R.raw.your_song);

OTHER TIPS

Try this :

public class LaunchScreen extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    MusicPlayer.SoundPlayer(context, raw_id); ;// Try this

    Thread logoTimer = new Thread() {
        public void run(){
            try{
                int logoTimer = 0;
                while(logoTimer < 4000){
                    sleep(100);
                    logoTimer = logoTimer +100;
                };
                startActivity(new Intent("com.plambech.CLEARSCREEN"));
            } 

            catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            finally{
                finish();
            }
        }
    };

    logoTimer.start();
}
}

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top