我有 2 Activities
1 用于主菜单,另一个用于游戏窗口。
问题是当按下手机的主页按钮时,预测试“游戏窗口”Activity
进入onStop()
方法,此时一切正常,但是当我返回我的应用程序时它崩溃了。
我已经研究过这个问题,我认为这与保存 mplayer 对象的状态有关,因为日志说,我试图保存 mplayer 对象的状态,但应用程序仍然有同样的问题。
那我需要保存测试视图的所有变量吗?我很感激帮助。
第二个Activity的代码
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