我正在關注的新教程的教程,但是當我的代碼看起來與視頻中的人物看起來一樣時,它不起作用。Android MediaPlayer:啓動方法崩潰
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
Post Logcat輸出。 –
將logcat添加到原始帖子。第一次使用logcat,所以我不確定我是否發佈了正確的東西。 – TempAcc