为什么Media Player会导致我的应用程序因空指针异常而崩溃?

时间:2011-11-23 02:03:52

标签: android

我得到以下logcat输出:

11-22 20:57:01.394: E/MediaPlayer(1647): error (1, 0)
11-22 20:57:01.404: D/MediaPlayer(1647): create failed:
11-22 20:57:01.404: D/MediaPlayer(1647): java.io.IOException: Prepare failed.: status=0x1
11-22 20:57:01.404: D/MediaPlayer(1647):    at android.media.MediaPlayer.prepare(Native Method)
11-22 20:57:01.404: D/MediaPlayer(1647):    at android.media.MediaPlayer.create(MediaPlayer.java:668)
11-22 20:57:01.404: D/MediaPlayer(1647):    at com.jesse.abc.Apple.onCreate(Apple.java:25)
11-22 20:57:01.404: D/MediaPlayer(1647):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-22 20:57:01.404: D/MediaPlayer(1647):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-22 20:57:01.404: D/MediaPlayer(1647):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-22 20:57:01.404: D/MediaPlayer(1647):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-22 20:57:01.404: D/MediaPlayer(1647):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-22 20:57:01.404: D/MediaPlayer(1647):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 20:57:01.404: D/MediaPlayer(1647):    at android.os.Looper.loop(Looper.java:123)
11-22 20:57:01.404: D/MediaPlayer(1647):    at android.app.ActivityThread.main(ActivityThread.java:3683)
11-22 20:57:01.404: D/MediaPlayer(1647):    at java.lang.reflect.Method.invokeNative(Native Method)
11-22 20:57:01.404: D/MediaPlayer(1647):    at java.lang.reflect.Method.invoke(Method.java:507)
11-22 20:57:01.404: D/MediaPlayer(1647):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-22 20:57:01.404: D/MediaPlayer(1647):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-22 20:57:01.404: D/MediaPlayer(1647):    at dalvik.system.NativeStart.main(Native Method)
11-22 20:57:01.414: D/AndroidRuntime(1647): Shutting down VM
11-22 20:57:01.414: W/dalvikvm(1647): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-22 20:57:01.414: E/AndroidRuntime(1647): FATAL EXCEPTION: main
11-22 20:57:01.414: E/AndroidRuntime(1647): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jesse.abc/com.jesse.abc.Apple}: java.lang.NullPointerException
11-22 20:57:01.414: E/AndroidRuntime(1647):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-22 20:57:01.414: E/AndroidRuntime(1647):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-22 20:57:01.414: E/AndroidRuntime(1647):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-22 20:57:01.414: E/AndroidRuntime(1647):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-22 20:57:01.414: E/AndroidRuntime(1647):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 20:57:01.414: E/AndroidRuntime(1647):     at android.os.Looper.loop(Looper.java:123)
11-22 20:57:01.414: E/AndroidRuntime(1647):     at android.app.ActivityThread.main(ActivityThread.java:3683)
11-22 20:57:01.414: E/AndroidRuntime(1647):     at java.lang.reflect.Method.invokeNative(Native Method)
11-22 20:57:01.414: E/AndroidRuntime(1647):     at java.lang.reflect.Method.invoke(Method.java:507)
11-22 20:57:01.414: E/AndroidRuntime(1647):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
11-22 20:57:01.414: E/AndroidRuntime(1647):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
11-22 20:57:01.414: E/AndroidRuntime(1647):     at dalvik.system.NativeStart.main(Native Method)
11-22 20:57:01.414: E/AndroidRuntime(1647): Caused by: java.lang.NullPointerException
11-22 20:57:01.414: E/AndroidRuntime(1647):     at com.jesse.abc.Apple.onCreate(Apple.java:32)
11-22 20:57:01.414: E/AndroidRuntime(1647):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-22 20:57:01.414: E/AndroidRuntime(1647):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-22 20:57:01.414: E/AndroidRuntime(1647):     ... 11 more
11-22 20:57:02.934: I/Process(1647): Sending signal. PID: 1647 SIG: 9

这是我的代码:

我非常确定我的Manifest和布局文件还可以。错误指向第32行,即myPlayer.start();。只有在测试期间反复在活动之间来回切换时才会发生崩溃。任何帮助将不胜感激。

package com.jesse.abc;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class Car extends Activity{

MediaPlayer mPlayer;
MediaPlayer myPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cletter);

    mPlayer = MediaPlayer.create(this, R.raw.button_sound1);
    myPlayer = MediaPlayer.create(this, R.raw.carsound);
    final Button bButton = (Button)findViewById(R.id.cbackbutton);
    final Button nButton = (Button)findViewById(R.id.cnextbutton);
    final ImageButton cButton = (ImageButton)findViewById(R.id.cButton);
    final Context ctx = this;

    myPlayer.start();



    bButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            mPlayer.start();
            Intent myIntent = new Intent(v.getContext(), Ball.class);
            ctx.startActivity(myIntent);
            myPlayer.release();

        }
    });

    nButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            mPlayer.start();
            Intent myIntent2 = new Intent(v.getContext(), Duck.class);
            ctx.startActivity(myIntent2);
            myPlayer.release();

        }
    });

    cButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            myPlayer.start();

        }
    });


}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub

    if(keyCode==KeyEvent.KEYCODE_BACK){

        this.startActivity(new Intent(Car.this, StartingPoint.class));
        myPlayer.release();
        finish();

    }
    return true;
}



}

2 个答案:

答案 0 :(得分:3)

是的,它肯定会崩溃。

        myPlayer.release();
        myPlayer=null;
        myPlayer.start();

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    myPlayer.release();
    myPlayer=null;
    finish();
}

您的对象myPlayernull,因此崩溃了:)

答案 1 :(得分:0)

你刚回答了自己的问题。 myPlayer为null。调试并一步一步到达该行,将鼠标悬停在myPlayer上,是否为null?你说在进出你的应用程序时会发生这种情况,你可能需要切换,然后重新进入,然后调试到该行。