0
請給我建議,如何糾正這個錯誤,開始的MediaPlayer - 在語句點擊錯誤mp.Start() - 空異常埃羅
這是因爲Android的構建目標的,它設置爲4.2.2
logcat的
05-01 02:31:01.081: E/AndroidRuntime(1124): Caused by: java.lang.NullPointerException
05-01 02:31:01.081: E/AndroidRuntime(1124): at com.vpl.ramrevhashu.medicaplayerfunmeth.Splash.onCreate(Splash.java:19)
05-01 02:31:01.081: E/AndroidRuntime(1124): at android.app.Activity.performCreate(Activity.java:5008)
05-01 02:31:01.081: E/AndroidRuntime(1124): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-01 02:31:01.081: E/AndroidRuntime(1124): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-01 02:31:01.081: E/AndroidRuntime(1124): ... 11 more
05-01 02:31:08.240: I/Process(1124): Sending signal. PID: 1124 SIG: 9<
代碼
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Splash extends Activity{
MediaPlayer ourSound;
@Override
protected void onCreate(Bundle SplashPage) {
// TODO Auto-generated method stub
super.onCreate(SplashPage);
setContentView(R.layout.splash);
ourSound = new MediaPlayer();
ourSound = MediaPlayer.create(Splash.this, R.raw.song);
ourSound.start(); //(Error at this stmt)
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch(InterruptedException e){
e.printStackTrace();
} finally{
Intent startPage = new Intent("com.vpl.ramrevhashu.medicaplayerfunmeth");
startActivity(startPage);
}
}
};
timer.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSound.release();
finish();
}
}
' ourSound'爲null,因爲這個'ourSound = MediaPlayer.create(Splash.this,R.raw.song)'失敗。你也不需要'ourSound = new MediaPlayer();' – Raghunandan