Android 尝试在目标活动中获取ParcelableArray时出现运行时异常

Android 尝试在目标活动中获取ParcelableArray时出现运行时异常,android,bundle,parcelable,runtimeexception,Android,Bundle,Parcelable,Runtimeexception,我正在尝试将自定义对象数组传递给活动。我已将parcelable实现为: public class WidgetState { static class Light implements Parcelable { int id; String text; int offColor,onColor; boolean on=false; boolean isRows; int size

我正在尝试将自定义对象数组传递给活动。我已将parcelable实现为:

public class WidgetState {

    static class Light implements Parcelable
    {
        int id;
        String text;
        int offColor,onColor;
        boolean on=false;

        boolean isRows;
        int size;

        public static final Parcelable.Creator<Light> CREATOR = new Parcelable.Creator<Light>() {
        public Light createFromParcel(Parcel in) {
            return new Light(in);
        }

        public Light[] newArray(int size) {
            return new Light[size];
        }
    };
        @Override
        public int describeContents() {
            return 0;
        }

        public Light(Parcel src)
        {
            id = src.readInt();
            text = src.readString();
            offColor = src.readInt();
            onColor = src.readInt();
            on = src.readInt()==1;
            isRows = src.readInt()==1;
            size = src.readInt();
        }

        public Light() { }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(id);
            dest.writeString(text);
            dest.writeInt(offColor);
            dest.writeInt(onColor);
            dest.writeInt(on?1:0);
            dest.writeInt(isRows?1:0);
            dest.writeInt(size);            
        }       
    }
}
并在生成的活动中通过

WidgetState.Light light = (WidgetState.Light)getIntent().getExtras().getParcelable("light")
但是当像这样包装和排列的时候

bundle.putParcelableArray(new WidgetState.Light[4],"lights");
在第一个活动中,我可以做得很好

WidgetState.Light[] lights = (WidgetState.Light[])bundle.getParcelableArray("lights");
intent.putExtras(bundle);
startActivityForResult(intent,1);
Intent intent = new Intent(MainActivity.this,GuiActivity.class);

Bundle bundle = new Bundle();
bundle.putParcelable("light", new WidgetState.Light());
bundle.putParcelableArray("lights", new WidgetState.Light[4]);                  

WidgetState.Light[]lights = (WidgetState.Light[])bundle.getParcelableArray("lights");

intent.putExtras(bundle);

startActivityForResult(intent,1);
但是在第二个活动中,当我调用

WidgetState.Light [] lights = (WidgetState.Light []) state.getParcelableArray("lights");
以下是第一个活动中的所有代码

WidgetState.Light[] lights = (WidgetState.Light[])bundle.getParcelableArray("lights");
intent.putExtras(bundle);
startActivityForResult(intent,1);
Intent intent = new Intent(MainActivity.this,GuiActivity.class);

Bundle bundle = new Bundle();
bundle.putParcelable("light", new WidgetState.Light());
bundle.putParcelableArray("lights", new WidgetState.Light[4]);                  

WidgetState.Light[]lights = (WidgetState.Light[])bundle.getParcelableArray("lights");

intent.putExtras(bundle);

startActivityForResult(intent,1);
第二个呢

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gui);      

    Bundle state = (savedInstanceState!=null)?savedInstanceState:getIntent().getExtras();

    try {
        WidgetState.Light light = (WidgetState.Light) state.getParcelable("light");
        // Throws RuntimeException on next line
        WidgetState.Light [] lights = (WidgetState.Light []) state.getParcelableArray("lights");

        Toast.makeText(this, "Good bundle", Toast.LENGTH_SHORT).show();

    }
    catch ( RuntimeException e)
    {
        Toast.makeText(this, "Failed to read bundle", Toast.LENGTH_SHORT).show();
    }
}

我遗漏了什么?

也发布LogCat错误,在catch块中使用
e.printStackTrace()
也可能有帮助。问题出在哪里?您可以在这里找到解决方案: