当我尝试在活动中播放音乐时,我的应用程序崩溃了

时间:2014-03-04 12:50:31

标签: android android-mediaplayer logcat

我创建了一个名为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();
    }
}

但是当我尝试从我的活动中访问它时,我的应用程序一直在崩溃。

我只是在onCreate:

中调用它
MusicPlayer.player.start();

我有log cat错误,我可以看到它抱怨了; MusicPlayer.player.start();在我的活动中。但我似乎无法理解错误。

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

我希望你能帮助我。并且发现,我做错了什么。

每个请求的LaunchActivity:

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();
    }
}

2 个答案:

答案 0 :(得分:3)

您必须在使用之前初始化MusicPlayer类:

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

答案 1 :(得分:2)

试试这个:

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();
}
}

希望这有帮助。