我正在构建我的第一个 android 应用程序,它是一个棋盘游戏,由 ImageViews(可拖动)、ToggleButton(打开/关闭声音)、TextViews(用于得分)、ArrayList、布尔值、整数、字符串、浮点数和一些双打组成。如果我按返回按钮进入主菜单,并尝试返回我正在玩的游戏,它就会崩溃。我没有实现任何 onPause、onStop、onRestart、onResume。我只实现了onCreate。我浏览了该网站上的几篇文章,但所有答案都令人困惑。我应该使用 onPause 和 onResume 或 onSaveInstanceState 和 onRestoreInstanceState 来保存我当前的游戏状态(所有数据类型和对象)。一个例子将不胜感激。
提前致谢!!
我的日志猫
09-12 16:13:06.326: E/AndroidRuntime(14207): FATAL EXCEPTION: main
09-12 16:13:06.326: E/AndroidRuntime(14207): java.lang.RuntimeException: Unable to resume activity {com.example.baghchalNepal/com.example.baghchal.UserAsTiger}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2851)
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2880)
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2302)
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.app.ActivityThread.access$700(ActivityThread.java:152)
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.os.Handler.dispatchMessage(Handler.java:99)
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.os.Looper.loop(Looper.java:137)
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.app.ActivityThread.main(ActivityThread.java:5328)
09-12 16:13:06.326: E/AndroidRuntime(14207): at java.lang.reflect.Method.invokeNative(Native Method)
09-12 16:13:06.326: E/AndroidRuntime(14207): at java.lang.reflect.Method.invoke(Method.java:511)
09-12 16:13:06.326: E/AndroidRuntime(14207): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-12 16:13:06.326: E/AndroidRuntime(14207): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-12 16:13:06.326: E/AndroidRuntime(14207): at dalvik.system.NativeStart.main(Native Method)
09-12 16:13:06.326: E/AndroidRuntime(14207): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.view.ViewGroup.addViewInner(ViewGroup.java:3435)
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.view.ViewGroup.addView(ViewGroup.java:3306)
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.view.ViewGroup.addView(ViewGroup.java:3251)
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.view.ViewGroup.addView(ViewGroup.java:3227)
09-12 16:13:06.326: E/AndroidRuntime(14207): at com.example.baghchal.UserAsTiger.makeGoatMove(UserAsTiger.java:683)
09-12 16:13:06.326: E/AndroidRuntime(14207): at com.example.baghchal.UserAsTiger.onResume(UserAsTiger.java:297)
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1202)
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.app.Activity.performResume(Activity.java:5328)
09-12 16:13:06.326: E/AndroidRuntime(14207): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2841)
09-12 16:13:06.326: E/AndroidRuntime(14207): ... 12 more
Java 代码
// create ImageView object to store a goat.
ImageView thisGoat = null;
// This method call receives safest position where a goat can be placed.
Point bestPoint = getBestPointToPlaceGoat();
// if there are goats remained to be placed on the board, select a goat to be moved to the board.
for (int i=0; i<imageArrayList.size(); i++) {
if ((imageArrayList.get(i).getX() == (xMin+70)) && (imageArrayList.get(i).getY() == (yMin-200))){
thisGoat = imageArrayList.get(i);
break;
}
}
// move the goat, if a goat was selected from above.
if (thisGoat != null) {
// Here I am removing the ImageView obj
relativeLayout.removeView(thisGoat);
thisGoat.setX(bestPoint.getX()-70);
thisGoat.setY(bestPoint.getY()-70);
bestPoint.setOccupiedBy("goat");
// this is where error is acc. to logcat
relativeLayout.addView(thisGoat);
}