gpt4 book ai didi

安卓媒体播放器 : start method crashing

转载 作者:行者123 更新时间:2023-11-30 02:13:59 24 4
gpt4 key购买 nike

我正在学习 thenewboston 的教程,但我遇到了一个问题,虽然我的代码看起来与视频中那个人的代码相同,但它不起作用。

public class Splash extends Activity{

MediaPlayer ourSong;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openStartingPoint = new Intent("com.example.tutorial.STARTINGPOINT");
startActivity(openStartingPoint);
}
}
};
timer.start();
}

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

我试着把//放在涉及 MediaPlayer 的每一行之前,然后一个一个地取消注释,看起来程序在到达 ourSong.start() 时崩溃了。

我不知道为什么会这样。有人可以帮助我吗?

这是视频:https://www.youtube.com/watch?v=-zGS_zrL0rY&index=17&list=PLEFA63803DBD4C83A

Logcat 输出:

04-16 16:40:18.964: I/art(1829): Background partial concurrent mark sweep GC freed 91(16KB) AllocSpace objects, 0(0B) LOS objects, 28% free, 10MB/14MB, paused 12.573ms total 114.113ms

04-16 16:40:19.067: E/MediaPlayer(1829): Should have subtitle controller already set

04-16 16:40:19.070: D/AndroidRuntime(1829): Shutting down VM

04-16 16:40:19.090: E/AndroidRuntime(1829): FATAL EXCEPTION: main

04-16 16:40:19.090: E/AndroidRuntime(1829): Process: com.example.tutorial, PID: 1829

04-16 16:40:19.090: E/AndroidRuntime(1829): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tutorial/com.example.tutorial.Splash}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference

04-16 16:40:19.090: E/AndroidRuntime(1829): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)

04-16 16:40:19.090: E/AndroidRuntime(1829): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)

04-16 16:40:19.090: E/AndroidRuntime(1829): at android.app.ActivityThread.access$800(ActivityThread.java:151)

04-16 16:40:19.090: E/AndroidRuntime(1829): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)

04-16 16:40:19.090: E/AndroidRuntime(1829): at android.os.Handler.dispatchMessage(Handler.java:102)

04-16 16:40:19.090: E/AndroidRuntime(1829): at android.os.Looper.loop(Looper.java:135)

04-16 16:40:19.090: E/AndroidRuntime(1829): at android.app.ActivityThread.main(ActivityThread.java:5257)

04-16 16:40:19.090: E/AndroidRuntime(1829): at java.lang.reflect.Method.invoke(Native Method)

04-16 16:40:19.090: E/AndroidRuntime(1829): at java.lang.reflect.Method.invoke(Method.java:372)

04-16 16:40:19.090: E/AndroidRuntime(1829): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)

04-16 16:40:19.090: E/AndroidRuntime(1829): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

04-16 16:40:19.090: E/AndroidRuntime(1829): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference

04-16 16:40:19.090: E/AndroidRuntime(1829): at com.example.tutorial.Splash.onCreate(Splash.java:17)

04-16 16:40:19.090: E/AndroidRuntime(1829): at android.app.Activity.performCreate(Activity.java:5990)

04-16 16:40:19.090: E/AndroidRuntime(1829): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)

04-16 16:40:19.090: E/AndroidRuntime(1829): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)

04-16 16:40:19.090: E/AndroidRuntime(1829): ... 10 more

04-16 16:40:28.664: I/Process(1829): Sending signal. PID: 1829 SIG: 9

最佳答案

您的媒体播放器对象未初始化。为空!

ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);我们的歌曲.start();

关于安卓媒体播放器 : start method crashing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29680691/

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