2013-05-08 3 views
0

메인 메뉴는 2 Activities이고 게임 창은 1입니다.onSaveInstanceState() 메소드에 몇 가지 저장해야합니까?

핸드폰의 홈 버튼이 사전 테스트 "게임 창"으로 푸시 될 때 문제가 발생합니다. 은 onStop() 메쏘드로 이동합니다.이 시점에서 모두 괜찮습니다.하지만 내 앱으로 돌아 가면 충돌합니다.

문제점을 조사한 결과 mplayer 객체의 상태를 저장하려고 시도했기 때문에 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); 

     } 

    } 

    } 

오류 로그의 코드 : 당신의 onPause는 MediaPlayer을 풀어줄에서

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 
+0

onPause가 호출되면 MediaPlayer 인스턴스가 보유한 리소스를 해제하고 나중에 응용 프로그램으로 돌아올 때 이전 인스턴스에서 seekTo 메서드를 호출하려고합니다! – WeNeigh

답변

0

. 서브 시스템에서 링크 된 모든 자원을 제거합니다.

@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; 
    } 

} 

그러나 당신의 이력서를 변경 :

이 같은해야 당신이 실제로 onStop에서 releaseonStart에 만들어야으로,

@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(); 
} 

당신이 좀 더 조정할 수 .

pauseplayonStart에있는 방법은 실제로 앱을 종료하지 않는 한 참조를 유지합니다. 앞뒤로 조금 더 부드럽게 진행해야합니다.

+0

고마워요! , 하나의 마지막 질문, 그래서 보호 된 void onSaveInstanceState (Bundle estadoguardado) 메서드는 onDestroy 메서드를 호출 할 때만 사용됩니다. –

+0

예, 내 기억에서 'onDestroy'전에 호출되었습니다. 그리고 onRestore는 onCreate 뒤에 호출됩니다. –