我有2 Activities
1主菜單和另一個遊戲窗口。我需要在onSaveInstanceState()方法中保存多少東西?
的問題是,當手機的home鍵被按下前測「遊戲窗口」 Activity
去onStop()
方法,在這一點上都可以,但是當我回到我的應用程序崩潰。
我研究了這個問題,我認爲它與保存mplayer對象的狀態有關,因爲日誌說了什麼,我試圖保存mplayer對象的狀態,但應用程序仍然有同樣的問題。
我需要保存測試視圖的所有變量嗎?我很感激幫助。
第二活動
public class pretest extends Activity {
private MediaPlayer mplayer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.Layou tParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Toast.makeText(this, "onCreate2", Toast.LENGTH_SHORT).show();
if(mplayer!=null){mplayer.release();}
mplayer=MediaPlayer.create(this, R.raw.forest);
View test = new test(this);
mplayer.seekTo(0);
mplayer.setLooping(true);
mplayer.start();
setContentView(test);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
mostrarSalir();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onStart(){
super.onStart();
Toast.makeText(this, "onStart2", Toast.LENGTH_SHORT).show();
}
@Override
public void onStop(){
super.onStop();
Toast.makeText(this, "onStop2", Toast.LENGTH_SHORT).show();
}
@Override
public void onRestart(){
super.onRestart();
Toast.makeText(this, "onRestart2", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy(){
super.onDestroy();
Toast.makeText(this, "onDestroy2", Toast.LENGTH_SHORT).show();
}
@Override
public void onPause(){
super.onPause();
Toast.makeText(this, "onPause2", Toast.LENGTH_SHORT).show();
//LIBERA A MEDIA PLAYER
if(mplayer!=null){mplayer.release();}
}
private void mostrarSalir(){
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("¿Desea Regresar al menu principal?");
dialog.setCancelable(false);
dialog.setPositiveButton("Si", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
dialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast toast1 =
Toast.makeText(getApplicationContext(),
":D!", Toast.LENGTH_SHORT);
toast1.show();
dialog.cancel();
}
});
dialog.show();
}
@Override
public void onResume(){
super.onResume();
//reinicia
Toast.makeText(this, "onResume2", Toast.LENGTH_SHORT).show();
mplayer.seekTo(0);
mplayer.start();
}
@Override
protected void onSaveInstanceState(Bundle estadoguardado){
super.onSaveInstanceState(estadoguardado);
if(mplayer!=null){
int pos=mplayer.getCurrentPosition();
estadoguardado.putInt("posicion", pos);
}
}
@Override
protected void onRestoreInstanceState(Bundle estadoguardado)
{
super.onRestoreInstanceState(estadoguardado);
if(estadoguardado!=null&&mplayer!=null){
int pos=estadoguardado.getInt("posicion");
mplayer.seekTo(pos);
}
}
}
錯誤日誌的代碼:
05-08 02:55:32.036: E/AndroidRuntime(7904): FATAL EXCEPTION: main
05-08 02:55:32.036: E/AndroidRuntime(7904): java.lang.RuntimeException: Unable to resume activity {com.example.brain/com.example.brain.pretest}: java.lang.IllegalStateException
05-08 02:55:32.036: E/AndroidRuntime(7904): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2214)
05-08 02:55:32.036: E/AndroidRuntime(7904): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2229)
05-08 02:55:32.036: E/AndroidRuntime(7904): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1019)
05-08 02:55:32.036: E/AndroidRuntime(7904): at android.os.Handler.dispatchMessage(Handler.java:130)
05-08 02:55:32.036: E/AndroidRuntime(7904): at android.os.Looper.loop(SourceFile:351)
05-08 02:55:32.036: E/AndroidRuntime(7904): at android.app.ActivityThread.main(ActivityThread.java:3814)
05-08 02:55:32.036: E/AndroidRuntime(7904): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 02:55:32.036: E/AndroidRuntime(7904): at java.lang.reflect.Method.invoke(Method.java:538)
05-08 02:55:32.036: E/AndroidRuntime(7904): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
05-08 02:55:32.036: E/AndroidRuntime(7904): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:659)
05-08 02:55:32.036: E/AndroidRuntime(7904): at dalvik.system.NativeStart.main(Native Method)
05-08 02:55:32.036: E/AndroidRuntime(7904): Caused by: java.lang.IllegalStateException
05-08 02:55:32.036: E/AndroidRuntime(7904): at android.media.MediaPlayer.seekTo(Native Method)
05-08 02:55:32.036: E/AndroidRuntime(7904): at com.example.brain.pretest.onResume(pretest.java:135)
05-08 02:55:32.036: E/AndroidRuntime(7904): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1189)
05-08 02:55:32.036: E/AndroidRuntime(7904): at android.app.Activity.performResume(Activity.java:3896)
05-08 02:55:32.036: E/AndroidRuntime(7904): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2204)
05-08 02:55:32.036: E/AndroidRuntime(7904): ... 10 more
當onPause被調用時,您將釋放MediaPlayer實例持有的資源,並且稍後再回到應用程序時,您正嘗試調用舊實例的seekTo方法! – WeNeigh 2013-05-08 10:12:22