gpt4 book ai didi

android - 将对象保存到 bandle 时出错

转载 作者:行者123 更新时间:2023-11-29 00:25:09 25 4
gpt4 key购买 nike

我想在我的手机旋转时保存我的类(class)。我的代码是:

public class MainActivity extends FragmentActivity
{
private MapCanvas mapCanvas = null;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

if(savedInstanceState == null)
{
this.mapCanvas = new MapCanvas(this);
}
else
{
this.mapCanvas = (MapCanvas)savedInstanceState.getSerializable("s");
}

setContentView(this.mapCanvas);
this.mapCanvas.requestFocus();
}

@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putSerializable("s", this.mapCanvas);
}
}

但是当我旋转手机时,setContentView(this.mapCanvas);

行出现错误

我的 MapCanvas 是:

public class MapCanvas extends ImageView implements Serializable
{
private static final long serialVersionUID = 1L;

....
}

我的聊天记录是:

11-27 20:09:37.969: W/dalvikvm(14743): threadid=1: thread exiting with uncaught exception (group=0x40187560)
11-27 20:09:37.999: E/AndroidRuntime(14743): FATAL EXCEPTION: main
11-27 20:09:37.999: E/AndroidRuntime(14743): java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.pangan.yazdbakishni/ir.pangan.yazdbakishni.activities.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
11-27 20:09:37.999: E/AndroidRuntime(14743): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
11-27 20:09:37.999: E/AndroidRuntime(14743): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
11-27 20:09:37.999: E/AndroidRuntime(14743): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2844)
11-27 20:09:37.999: E/AndroidRuntime(14743): at android.app.ActivityThread.access$1600(ActivityThread.java:121)
11-27 20:09:37.999: E/AndroidRuntime(14743): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:947)
11-27 20:09:37.999: E/AndroidRuntime(14743): at android.os.Handler.dispatchMessage(Handler.java:99)
11-27 20:09:37.999: E/AndroidRuntime(14743): at android.os.Looper.loop(Looper.java:130)
11-27 20:09:37.999: E/AndroidRuntime(14743): at android.app.ActivityThread.main(ActivityThread.java:3768)
11-27 20:09:37.999: E/AndroidRuntime(14743): at java.lang.reflect.Method.invokeNative(Native Method)
11-27 20:09:37.999: E/AndroidRuntime(14743): at java.lang.reflect.Method.invoke(Method.java:507)
11-27 20:09:37.999: E/AndroidRuntime(14743): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
11-27 20:09:37.999: E/AndroidRuntime(14743): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
11-27 20:09:37.999: E/AndroidRuntime(14743): at dalvik.system.NativeStart.main(Native Method)
11-27 20:09:37.999: E/AndroidRuntime(14743): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
11-27 20:09:37.999: E/AndroidRuntime(14743): at android.view.ViewGroup.addViewInner(ViewGroup.java:1976)
11-27 20:09:37.999: E/AndroidRuntime(14743): at android.view.ViewGroup.addView(ViewGroup.java:1871)
11-27 20:09:37.999: E/AndroidRuntime(14743): at android.view.ViewGroup.addView(ViewGroup.java:1851)
11-27 20:09:37.999: E/AndroidRuntime(14743): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:226)
11-27 20:09:37.999: E/AndroidRuntime(14743): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:216)
11-27 20:09:37.999: E/AndroidRuntime(14743): at android.app.Activity.setContentView(Activity.java:1668)
11-27 20:09:37.999: E/AndroidRuntime(14743): at ir.pangan.yazdbakishni.activities.MainActivity.onCreate(MainActivity.java:37)
11-27 20:09:37.999: E/AndroidRuntime(14743): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-27 20:09:37.999: E/AndroidRuntime(14743): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
11-27 20:09:37.999: E/AndroidRuntime(14743): ... 12 more

最佳答案

您不能将控件存储在一个包中。您必须在每次 onCreate 调用时重新创建所有 View ,但您可以保存该自定义 View 的内部状态。这也可以使用自定义构造函数恢复。

在这里看我的例子:

public class MapCanvas extends ImageView {
public MapCanvas(Context c, Bundle state) {
super(c);
recoverState(state);
}

public recoverState(Bundle state) {
if(state==null) {
return;
}
// read values...
}

public Bundle saveState(Bundle outState) {
// save whatever you want.
}
}

这里是您修改后的 Activity :

public class MainActivity extends FragmentActivity {           
private MapCanvas mapCanvas = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mapCanvas = new MapCanvas(this, savedInstanceState);

setContentView(mapCanvas);
mapCanvas.requestFocus();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapCanvas.saveState(outState);
}
}

关于android - 将对象保存到 bandle 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20248015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com