传递包含Parcelable对象数组的Parcelable对象

时间:2011-09-12 10:16:03

标签: android android-activity parcelable

我想传递一个包含Parcelable对象数组的Parcelable对象;我的问题是,尽管第二个活动开始,返回对象为null。我用这种方式调用Activity:

i=new Intent(SoNActivity.this,OperationListAct.class);
i.putExtra("1", opSettings);
startActivityForResult(i, CommunicationConfig.getRequestCode(SoNActivity.class));

opSetting对象是OperationSetting类的一个实例,它具有as属性作为对象数组:

public class OperationSetting implements Parcelable{
    public class OperationInfo implements Parcelable{
            private Operation mOperation;
            private boolean mActive;
            private int mPosition;
            private String mName;
            private String mDescription;


            public OperationInfo(boolean isAct,int pos,String name, String descr,Operation op){
                    mActive=isAct;
                    mPosition =pos;
                    mName=name;
                    mDescription=descr;
                    mActive=true;
                    mOperation=op;
            }

            public Operation getOperation(){return mOperation;}
            public boolean getActive(){return mActive;}
            public int getPosition(){return mPosition;}
            public String getName(){return mName;}
            public String getDescription(){return mDescription;}

            public void setOperation(Operation operation){mOperation=operation;}
            public void setActive(boolean active){mActive=active;}
            public void setPosition(int position){mPosition=position;}
            public void setName(String name){mName=name;}
            public void setDescription(String description){mDescription=description;}

            private String[] getAttList(){
                    String[] atts={"name","description","position","isEnabled","operation"};
                    return atts;
            }



            public int describeContents() {
                    return 0;
            }

            @Override
            public void writeToParcel(Parcel out, int flags) {
                    Bundle bndl=new Bundle();
                    String[] attList=this.getAttList();
                    bndl.putString(attList[0], mName);
                    bndl.putString(attList[1],mDescription);
                    bndl.putInt(attList[2],mPosition);
                    bndl.putBoolean(attList[3], mActive);
                    bndl.putString(attList[4], mOperation.toString());
                    out.writeBundle(bndl);

            }

        public OperationInfo (Parcel in) {
            Bundle bndl = in.readBundle();
            String[] attList=this.getAttList();
            mName=bndl.getString(attList[0]);
            mDescription=bndl.getString(attList[1]);
            mPosition=bndl.getInt(attList[2]);
            mActive=bndl.getBoolean(attList[3]);
            mOperation=Operation.valueOf(bndl.getString(attList[4]));
        }

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

                 public OperationInfo[] newArray(int size) {
                     return new OperationInfo[size];
                 }
        };


    }

    private OperationInfo[] m_settings;



    public OperationSetting (String[] namesOp,String[] descrOp,int[] positions, boolean[] enabled,boolean firstExec){
            Operation[] opList=Operation.values();
            m_settings=new OperationInfo[opList.length];
            for(int i=0;i<opList.length;i++){
                    m_settings[i]=new OperationInfo(enabled[i], positions[i], namesOp[i], descrOp[i], opList[i]);
            }
            if(firstExec){
                    String[] newOrder=namesOp.clone();
                    Arrays.sort(newOrder);
                    this.sort(newOrder);
            }
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
            dest.writeParcelableArray(m_settings, flags);
    }

public OperationSetting(Parcel in) {
    m_settings = (OperationInfo[]) in.readParcelableArray(OperationInfo.class.getClassLoader());

}

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

    public OperationSetting[] newArray(int size) {
            return new OperationSetting[size];
    }
};}

最后,从我写的第二个Activity中收回对象:

Bundle b=getIntent().getExtras();
            OperationSetting op=b.getParcelable("1");

第二个Activity运行正常,但是当我尝试获取对象时,我得到一个java.lang.outOfMemory Exception;它在方法OperationSetting(Parcel in)上崩溃了。怎么了?

1 个答案:

答案 0 :(得分:0)

我不确定你所尝试的是否能够正常工作,因为如果你的类实现了Parcelable,那么它应该有一个静态字段 CREATOR,而你的模型是不可能的,因为内部类不能有静态字段。如何将OperationInfo完全放在另一个类中,然后尝试?

OutOfMemory错误可能是由于对OperatorSettings(in)的递归调用导致堆栈溢出(giggle!)。