1
我想傳遞一個Parcelable對象,其中包含一個Parcelable對象數組;我的問題是,儘管第二個活動開始返回對象爲null。我所說的活性是這樣的:傳遞一個可包含對象,其中包含一個Parcelable對象數組
i=new Intent(SoNActivity.this,OperationListAct.class);
i.putExtra("1", opSettings);
startActivityForResult(i, CommunicationConfig.getRequestCode(SoNActivity.class));
的opSetting對象是OperationSetting類的一個實例,其具有作爲屬性對象的數組:
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];
}
};}
最後,從收回對象第二個活動我寫道:
Bundle b=getIntent().getExtras();
OperationSetting op=b.getParcelable("1");
第二個活動運行正常,但是當我試圖讓我得到一個java.lang.outOfMemory異常的對象;它在OperationSetting(Parcel in)方法上崩潰。它出什麼問題了?