我需要在onSaveInstanceState()方法中保存多少东西?

时间:2013-05-08 10:03:24

标签: java android eclipse

主菜单有2 Activities 1,游戏窗口有另一个。

问题是当手机的主页按钮被按下时,预测试“游戏窗口”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

1 个答案:

答案 0 :(得分:0)

在你的onPause中你发布了MediaPlayer。这很好地释放了它在子系统中链接的任何资源。

这应该是:

@Override
public void onPause()
{
    super.onPause();
    Toast.makeText(this, "onPause2", Toast.LENGTH_SHORT).show();
    //LIBERA A MEDIA PLAYER
    if (mplayer != null)
    {
        mplayer.release();
        mplayer = null;
    }

}

但将简历改为:

@Override
public void onResume()
{


    super.onResume();
    //reinicia
    Toast.makeText(this, "onResume2", Toast.LENGTH_SHORT).show();
    if (mplayer == null)
        mplayer = MediaPlayer.create(this, R.raw.forest);
    mplayer.seekTo(0);
    mplayer.setLooping(true);
    mplayer.start();
}

您可以稍微调整一下,因为您应该在release中实际onStop并在onStart中创建。

然后pause onPause playonStart中的{{1}},除非您实际离开应用,否则保留对该引用的引用。应该让后退和前进更顺畅。