0

我正在构建我的第一个 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);           
    }
4

2 回答 2

0

如果我按返回按钮进入主菜单,并尝试返回我正在玩的游戏,它就会崩溃。

如果唯一的问题是当您按下后退按钮时,如您所说,那么您可以覆盖后退按钮并将数据保存在那里

@Override
public void onBackPressed()
{
    // save your data here using SharedPrefs, DB, or however you need
}

您不需要实施onResume(),因为您Activity将被杀死并onCreate()会再次被调用,您可以在其中检查存储的数据并恢复您的值。

如果您担心另一个Activity出现在前台但当前Activity仍在堆栈中,那么您可以onPause()以相同的方式覆盖并将数据保存在该函数中。

如果这些没有帮助,那么正如 Prmths 的评论中所述,发布您的 logcat,以便我们查看错误是什么。

带有生命周期示例的活动文档

于 2013-09-12T20:55:41.420 回答
-1

这些方法将完全为您使用

您可以使用SharedPreferences保存数据并使用onResume将其取回 从此处了解更多信息

@Override
protected void onResume()
{       
    super.onResume();
}
@Override
protected void onPause()
{
    super.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
    // Save your data here
    super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
    super.onRestoreInstanceState(savedInstanceState);       
     // REtrieve your data heve
}

您将了解有关活动生命周期的更多信息

在此处输入图像描述

于 2013-09-12T20:56:17.707 回答