【发布时间】:2013-09-12 20:45:00
【问题描述】:
我正在构建我的第一个 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);
}
【问题讨论】:
-
- A:您应该使用 Activity 生命周期方法来保存任何数据并在 Activity 发生状态更改时清理正在使用的任何资源。 - B:你收到的错误是什么?你能发布一个 Logcat 吗?
-
你的意思是onPause,onStop,...通过Activity生命周期方法吗?我并没有真正使用任何特殊资源,只是我猜想通过存储数据类型和对象来存储我在问题中所述的 RAM。我在原始问题中添加了 logcat 错误。
-
根据您的 Logcat:错误位于 UserAsTiger 类的第 683 行。在 Logcat 中查找 Caused by: java.lang.IllegalStateException: The specified child already has a parent 的行。您必须先在孩子的父母上调用 removeView()。
-
检查一下,我添加了发生错误的代码的 sn-p,我在添加之前从布局中删除视图。
-
如何删除视图?在上面的代码中,错误中提到的那一行是哪一行?
标签: android android-activity state