Wrong state class error on first challenge


#1

I’m working on the first challenge and am implementing it by storing in the four pertinent values of each box into a float array in a bundle, and then pulling those out in on restore method. The following is the code for saving and restoring:

[code]@Override
protected Parcelable onSaveInstanceState() {
super.onSaveInstanceState();

    Bundle state = new Bundle();
    int i = 0;
    float originX, originY, currentX, currentY;

    for (Box b: boxes) {
        originX = b.getOrigin().x;
        originY = b.getOrigin().y;
        currentX = b.getCurrent().x;
        currentY = b.getCurrent().y;
        float[] array = {originX, originY, currentX, currentY};

        state.putFloatArray(Integer.toString(i), array);
        i++;
    }

    boxes.clear();
    return state;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
    super.onRestoreInstanceState(state);

    Bundle boxInfo = (Bundle) state;
    int i = 0;

    float[] boxPoints = boxInfo.getFloatArray(Integer.toString(i));

    while (boxPoints != null) {
        boxes.add(new Box(boxPoints[0], boxPoints[1], boxPoints[2], boxPoints[3]));

        i++;
        boxPoints = boxInfo.getFloatArray(Integer.toString(i));
    }

    invalidate();
}[/code]

And the stack trace that gets returned:

Caused by: java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.os.Bundle instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/drawing_view. Make sure other views do not use the same id. at android.view.View.onRestoreInstanceState(View.java:13645) at com.bluhmann.draganddraw.BoxDrawingView.onRestoreInstanceState(BoxDrawingView.java:126) at android.view.View.dispatchRestoreInstanceState(View.java:13621) at android.view.View.restoreHierarchyState(View.java:13599) at android.support.v4.app.Fragment.restoreViewState(Fragment.java:465) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:969) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108) at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1917) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:544) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220) at android.app.Activity.performStart(Activity.java:5949) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3912) at android.app.ActivityThread.access$900(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5223) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

BoxDrawingView.java:126 pertains to the super.onRestoreInstanceState(state); line. Any insights as to why this error is being thrown would be great. I’m virtually positive that I have no conflicting ID’s in anything, I’ve followed the book’s code for everything else except these two methods so far, and added on new constructor to the box class in order to just instantiate the different points right from the constructor, but that shouldn’t have added any new ID’s