gpt4 book ai didi

java - 致命异常 : main Caused by: java. playAudio 处的 lang.NullPointerException(extras.getInt(MEDIA));

转载 作者:行者123 更新时间:2023-12-01 13:42:11 25 4
gpt4 key购买 nike

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

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

最佳答案

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

您应该首先检查 extras 是否包含 key 。像这样的事情:

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

关于java - 致命异常 : main Caused by: java. playAudio 处的 lang.NullPointerException(extras.getInt(MEDIA));,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20644557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com