当我在活动上使用start()方法时,MediaPlayer会使活动崩溃

时间:2015-08-19 18:10:00

标签: android android-mediaplayer

我已尝试过多种方法在菜单屏幕上播放音乐(第一项活动)。我试过这个导致应用程序崩溃永远不会打开:

Mediaplayer mp;

@Override
protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Play music at the menu screen (main activity)
    mp = MediaPlayer.create(this, R.raw.menumusic);
    mp.start();
}

我试过这个可以防止一开始就崩溃但不播放任何音乐:

Mediaplayer mp;

@Override
protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Play music at the menu screen (main activity)
    mp = new MediaPlayer();
    mp.create(this, R.raw.menumusic);
    mp.start();
}

第一个崩溃日志:

    08-19 13:20:03.619  12759-12759/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
08-19 13:20:03.620  12759-12759/? E/android.os.Debug﹕ failed to load memtrack module: -2
08-19 13:20:03.784  12759-12773/? E/art﹕ Thread attaching while runtime is shutting down: Binder_2
08-19 13:20:04.533  12780-12780/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
08-19 13:20:04.535  12780-12780/? E/android.os.Debug﹕ failed to load memtrack module: -2
08-19 13:20:05.589    931-12814/? E/GenericSource﹕ Failed to init from data source!
08-19 13:20:05.592  12794-12810/? E/MediaPlayer﹕ error (1, -2147483648)
08-19 13:20:05.594  12794-12794/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.jason.dodge, PID: 12794
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jason.dodge/com.example.jason.dodge.MainActivity}: 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:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
            at com.example.jason.dodge.MainActivity.onCreate(MainActivity.java:31)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

第二个崩溃日志:

08-19 13:24:17.961  13075-13082/? E/art﹕ Failed sending reply to debugger: Broken pipe
08-19 13:24:18.095  13075-13089/? E/MediaPlayer﹕ error (1, -2147483648)
08-19 13:24:18.098  13075-13075/? E/MediaPlayer﹕ start called in state 1
08-19 13:24:18.098  13075-13075/? E/MediaPlayer﹕ error (-38, 0)
08-19 13:24:18.100  13075-13075/? E/MediaPlayer﹕ start called in state 0
08-19 13:24:18.202  13075-13075/? E/MediaPlayer﹕ Error (-38,0)

执行mp.prepare()时的日志,在mp.create之后用try / catch包围(this,R.raw.menumusic);线

08-19 13:37:58.455  18212-18226/com.example.jason.dodge E/MediaPlayer﹕ error (1, -2147483648)
08-19 13:37:58.456  18212-18212/com.example.jason.dodge E/MediaPlayer﹕ prepareAsync called in state 1
08-19 13:37:58.456  18212-18212/com.example.jason.dodge E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.jason.dodge, PID: 18212
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jason.dodge/com.example.jason.dodge.MainActivity}: java.lang.IllegalStateException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.IllegalStateException
            at android.media.MediaPlayer._prepare(Native Method)
            at android.media.MediaPlayer.prepare(MediaPlayer.java:1135)
            at com.example.jason.dodge.MainActivity.onCreate(MainActivity.java:33)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
它仍然说是由非法国家造成的。以为它的准备会解决这个问题,所以直到准备好的状态才会开始。

0 个答案:

没有答案