传递一个具有对象列表的parcelable对象?

时间:2014-07-24 05:55:48

标签: java android

您好我创建了一个类,它将一个"类的对象列表",一个字符串和字符串列表发送到一个Activity到另一个Activity,它对#34; String&" String& amp; ;字符串列表"但是在发送"任何类的对象列表时会产生错误"。

这是我的第一个活动。

FirstActivity.java

public class FirstActivity extends Activity {

List<String> teachersName;
SampleClass sampleClass;
List<SampleClass> studentDetails;

ParcebleDemo parcebleDemo;

Button button;

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

    teachersName = new ArrayList<>();
    studentDetails = new ArrayList<SampleClass>();

    teachersName.add("1111");
    teachersName.add("2222");
    teachersName.add("3333");
    teachersName.add("4444");
    teachersName.add("5555");

    for (int i = 0; i < 4; i++) {
        sampleClass = new SampleClass();
        sampleClass.name = "Raja" + " " + i;
        sampleClass.rollno = 63448 + (i * 10);
        studentDetails.add(sampleClass);
    }

    parcebleDemo = new ParcebleDemo();

    parcebleDemo.setAddress("koromangala");
    parcebleDemo.setTeacherName(teachersName);
    parcebleDemo.setSample(studentDetails);

    button =  (Button) findViewById(R.id.button);


    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Bundle bundle = new Bundle();
            bundle.putParcelable("Parceble", parcebleDemo);
            Intent intent =new Intent(FirstActivity.this, SecondActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);

        }
    });
  }
}

接收Parcelable日期的第二个Activity。

SecondActivity.java

public class SecondActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ParcebleDemo parcebleDemo =  getIntent().getExtras().getParcelable("Parceble");

    String address = parcebleDemo.getAddress();

    System.out.println("Address >> "+address);

    int sizeOfTeacher = parcebleDemo.getTeacherName().size();

    for (int i = 0; i < sizeOfTeacher; i++) {
        System.out.println("Teacher "+i+" >> "+ parcebleDemo.getTeacherName().get(i));
    }

    int sizeOfStudent = parcebleDemo.getSample().size();

    for (int i = 0; i < sizeOfStudent; i++) {
        System.out.println("StudentName "+i+" >> "+ parcebleDemo.getSample().get(i).name);
        System.out.println("StudentRoll "+i+" >> "+ parcebleDemo.getSample().get(i).rollno);
    }

  }
}

包含一些数据的类。

public class SampleClass {

    int rollno;
    String name;

}

包含Parceble实现的类是:

ParcebleDemo.java

public class ParcebleDemo implements Parcelable {

private String address;
private List<String> teacherName;
private List<SampleClass> sample;

public ParcebleDemo() {
    sample = new ArrayList<SampleClass>();

    teacherName = new ArrayList<String>();
}

public ParcebleDemo(Parcel source) {
    readFromParcel(source);
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public List<String> getTeacherName() {
    return teacherName;
}

public void setTeacherName(List<String> teacherName) {
    this.teacherName = teacherName;
}

public List<SampleClass> getSample() {
    return sample;
}

public void setSample(List<SampleClass> sample) {
    this.sample = sample;
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(address);
    dest.writeList(teacherName);
    dest.writeList(sample);

}

public static final Parcelable.Creator<ParcebleDemo> CREATOR = new Parcelable.Creator<ParcebleDemo>() {

    @Override
    public ParcebleDemo createFromParcel(Parcel source) {
        return new ParcebleDemo(source);
    }

    @Override
    public ParcebleDemo[] newArray(int size) {
        return new ParcebleDemo[size];
    }
};

public void readFromParcel(Parcel source) {
    address = source.readString();

    source.readList(teacherName, null);

    source.readList(sample, SampleClass.class.getClassLoader());

  }

}

因此,当我运行它时,会产生错误。

dest.writeList(sample); line.


07-24 05:35:44.000: E/AndroidRuntime(1490): FATAL EXCEPTION: main
07-24 05:35:44.000: E/AndroidRuntime(1490): Process: com.wtp, PID: 1490
07-24 05:35:44.000: E/AndroidRuntime(1490): java.lang.RuntimeException: Parcel: unable to marshal value com.parse.ParseObject@52db8c98
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.os.Parcel.writeValue(Parcel.java:1266)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.os.Parcel.writeList(Parcel.java:653)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at com.wtp.parserdata.EventEntitity.writeToParcel(EventEntitity.java:188)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.os.Parcel.writeParcelable(Parcel.java:1285)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.os.Parcel.writeValue(Parcel.java:1204)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.os.Parcel.writeArrayMapInternal(Parcel.java:618)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.os.Bundle.writeToParcel(Bundle.java:1692)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.os.Parcel.writeBundle(Parcel.java:636)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.content.Intent.writeToParcel(Intent.java:7013)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2076)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1419)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.app.Activity.startActivityForResult(Activity.java:3424)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.app.Activity.startActivityForResult(Activity.java:3385)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.app.Activity.startActivity(Activity.java:3627)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.app.Activity.startActivity(Activity.java:3595)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at com.wtp.ListPlanActivity$2.onItemClick(ListPlanActivity.java:168)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.widget.AdapterView.performItemClick(AdapterView.java:299)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.widget.AbsListView$3.run(AbsListView.java:3638)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.os.Handler.handleCallback(Handler.java:733)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.os.Looper.loop(Looper.java:136)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at java.lang.reflect.Method.invokeNative(Native Method)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at java.lang.reflect.Method.invoke(Method.java:515)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-24 05:35:44.000: E/AndroidRuntime(1490):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

根据演示类的http://parcelabler.com自动生成的Parcelable实现,它应该如下所示:

public class ParcebleDemo implements Parcelable
{
    private String address;
    private List<String> teacherName;
    private List<SampleClass> sample;

    public ParcebleDemo() {
        sample = new ArrayList<SampleClass>();

        teacherName = new ArrayList<String>();
    }

    protected ParcebleDemo(Parcel in) 
    {
        address = in.readString();
        if (in.readByte() == 0x01) {
            teacherName = new ArrayList<String>();
            in.readList(teacherName, String.class.getClassLoader());
        } else {
            teacherName = null;
        }
        if (in.readByte() == 0x01) {
            sample = new ArrayList<SampleClass>();
            in.readList(sample, SampleClass.class.getClassLoader());
        } else {
            sample = null;
        }
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(address);
        if (teacherName == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeList(teacherName);
        }
        if (sample == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeList(sample);
        }
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<ParcebleDemo> CREATOR = new Parcelable.Creator<ParcebleDemo>() {
        @Override
        public ParcebleDemo createFromParcel(Parcel in) {
            return new ParcebleDemo(in);
        }

        @Override
        public ParcebleDemo[] newArray(int size) {
            return new ParcebleDemo[size];
        }
    };
}