1

我正在尝试在方向更改时保存我的自定义视图(DrawView)。当方向改变时,代码工作得很好,但是当我最小化屏幕或按下设备上的主页按钮时,我得到堆栈溢出错误......你能帮忙吗?谢谢..

我的主要活动实现了这两种方法

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelable("drawView", drawView);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    drawView = savedInstanceState.getParcelable("drawView");
    super.onRestoreInstanceState(savedInstanceState);
}

我的自定义视图: DrawView 实现 Parcelable 并覆盖这些方法。

public class DrawView extends View implements OnTouchListener, Parcelable {
.........

@Override
public int describeContents() {
      return 0;
    }

@Override
public void writeToParcel(Parcel parcel, int i) {
        parcel.writeParcelable(this, i);
}

public void readFromParcel(Parcel in) {
        DrawView object1  = in.readParcelable(DrawView.class.getClassLoader());
}

我得到的异常是: E/AndroidRuntime: FATAL EXCEPTION: main java.lang.StackOverflowError DrawView.writeToParcel(DrawView.java:261) at android.os.Parcel.writeParcelable(Parcel.java:1254) DrawView.writeToParcel(DrawView .java:261) 在 android.os.Parcel.writeParcelable(Parcel.java:1254) DrawView.writeToParcel(DrawView.java:261) 在 android.os.Parcel.writeParcelable(Parcel.java:1254)

4

0 回答 0