致命异常:主要原因:playAudio上的java.lang.NullPointerException(extras.getInt(MEDIA));

时间:2013-12-17 21:00:15

标签: java android nullpointerexception mp3 forceclose

我正在尝试在我的应用程序中创建一个MP3播放器但是当我使用文件浏览器选择MP3时,然后使用我的应用程序打开它 - 由于行上的空指针异常,我得到强制关闭错误:< / p>

playAudio(extras.getInt(MEDIA));

如何避免这种情况?

JAVA:

public class MediaPlayerDemo extends Activity {

    private static final String TAG = "MediaPlayerDemo";
    private MediaPlayer mMediaPlayer;
    private static final String MEDIA = "media";
    private static final int LOCAL_AUDIO = 1;
    private static final int STREAM_AUDIO = 2;
    private static final int RESOURCES_AUDIO = 3;
    private static final int LOCAL_VIDEO = 4;
    private static final int STREAM_VIDEO = 5;
    private String path;

    private TextView tx;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        tx = new TextView(this);
        setContentView(tx);
        Bundle extras = getIntent().getExtras();
        playAudio(extras.getInt(MEDIA));
    }

    private void playAudio(Integer media) {
        try {
            switch (media) {
                case LOCAL_AUDIO:
                    /**
                     * TODO: Set the path variable to a local audio file path.
                     */
                    path = "";
                    if (path == "") {
                        // Tell the user to provide an audio file URL.
                        Toast
                                .makeText(
                                        MediaPlayerDemo.this,
                                        "Please edit MediaPlayer_Audio Activity, "
                                                + "and set the path variable to your audio file path."
                                                + " Your audio file must be stored on sdcard.",
                                        Toast.LENGTH_LONG).show();

                    }
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setDataSource(path);
                    mMediaPlayer.prepare();
                    mMediaPlayer.start();
                    break;
                case RESOURCES_AUDIO:
                    /**
                     * TODO: Upload a audio file to res/raw folder and provide
                     * its resid in MediaPlayer.create() method.
                     */
                    mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr);
                    mMediaPlayer.start();

            }
            tx.setText("Playing audio...");

        } catch (Exception e) {
            Log.e(TAG, "error: " + e.getMessage(), e);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // TODO Auto-generated method stub
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }

    }
}




12-17 15:55:09.377: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:09.385: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:09.390: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:09.393: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:16.234: I/fengjun1010(5461): --onServiceStateChanged,state=SIM1 0 home MetroPCS T-Mobile 310260  EDGE CSS not supported -1 -1 RoamInd=-1 DefRoamInd=-1 EmergOnly=false Regist state: 1
12-17 15:55:20.395: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:20.395: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:20.395: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:20.397: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:20.397: W/Trace(5461): Unexpected value from nativeGetEnabledTags: 0
12-17 15:55:20.398: I/fengjun1010(5461): -----agoldPhoneStateListener-----
12-17 15:55:20.398: I/fengjun1010(5461): ----------mPhoneListenerFlag=true
12-17 15:55:20.437: D/AndroidRuntime(5461): Shutting down VM
12-17 15:55:20.437: W/dalvikvm(5461): threadid=1: thread exiting with uncaught exception (group=0x412ca9a8)
12-17 15:55:20.446: E/AndroidRuntime(5461): FATAL EXCEPTION: main
12-17 15:55:20.446: E/AndroidRuntime(5461): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project/com.example.project.ui.phone.MediaPlayerDemo}: java.lang.NullPointerException
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.ActivityThread.access$600(ActivityThread.java:156)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.os.Looper.loop(Looper.java:153)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.ActivityThread.main(ActivityThread.java:5297)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at java.lang.reflect.Method.invokeNative(Native Method)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at java.lang.reflect.Method.invoke(Method.java:511)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at dalvik.system.NativeStart.main(Native Method)
12-17 15:55:20.446: E/AndroidRuntime(5461): Caused by: java.lang.NullPointerException
12-17 15:55:20.446: E/AndroidRuntime(5461):     at com.example.project.ui.phone.MediaPlayerDemo.onCreate(MediaPlayerDemo.java:31)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.Activity.performCreate(Activity.java:5262)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
12-17 15:55:20.446: E/AndroidRuntime(5461):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
12-17 15:55:20.446: E/AndroidRuntime(5461):     ... 11 more

1 个答案:

答案 0 :(得分:1)

最有可能的罪魁祸首是extras.getInt(MEDIA)。如果extras实际上不包含具有键MEDIA的对象,则它将返回null

您应首先检查extras是否包含密钥。像这样:

if( extras.containsKey( MEDIA ) ) {
    playAudio(extras.getInt(MEDIA);
} else {
    Toast.makeText(this, "No media provided in intent", Toast.LENGTH_SHORT).show();
}