Java Android-如何发送和接收嵌套的包裹对象”;捆绑;
我正在尝试通过捆绑包发送和接收另一个包裹对象中的包裹阵列对象 始终存在数组长度问题 代码:Parcelled ARRAY OBJECTS Author.javaJava Android-如何发送和接收嵌套的包裹对象”;捆绑;,java,android,Java,Android,我正在尝试通过捆绑包发送和接收另一个包裹对象中的包裹阵列对象 始终存在数组长度问题 代码:Parcelled ARRAY OBJECTS Author.java public class Author implements Parcelable { public String firstName; public String middleInitial; public String lastName; public Author() { } @Override public void wr
public class Author implements Parcelable {
public String firstName;
public String middleInitial;
public String lastName;
public Author() {
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(this.firstName);
out.writeString(this.middleInitial);
out.writeString(this.lastName);
}
private Author(Parcel in) {
this.firstName = in.readString();
this.middleInitial = in.readString();
this.lastName = in.readString();
}
public int describeContents(){
return 0;
}
public static final Parcelable.Creator<Author> CREATOR = new Parcelable.Creator<Author>() {
@Override
public Author createFromParcel(Parcel in) {
return new Author(in);
}
@Override
public Author[] newArray(int size) {
return new Author[size];
}
};
public String toString() {
StringBuffer sb = new StringBuffer();
if (firstName != null && !"".equals(firstName)) {
sb.append(firstName);
sb.append(' ');
}
if (middleInitial != null && !"".equals(middleInitial)) {
sb.append(middleInitial);
sb.append(' ');
}
if (lastName != null && !"".equals(lastName)) {
sb.append(lastName);
}
return sb.toString();
}
远程活动“BOOK Activity.CLASS”
任何想法,我如何解决这个问题。?嵌套的包裹成捆。另外,我确信我是否使用了正确的方法来读取author对象上的parcelable中的数组
谢谢,尝试改用packet.createTypedArray(类加载器…)。据我所见,您使用的是readTypedArray,但在尝试读入目标数组之前没有初始化它。为了做到这一点,您需要通过包裹传递数组的当前长度,并在读取之前将数组初始化为正确的大小。我尝试使用createTypedArray“this.authors=In.createTypedArray(Author.CREATOR);但我预期的结果是错误的..”作者名称开始转移到另一个变量中。我已将此“out.writeArray()”也更改为“out.writeTypedaray()”。我已将其修复
public class Book implements Parcelable {
public int id;
public String title;
public Author[] authors;
public String isbn;
public String price;
private Book(Parcel in) {
this.id = in.readInt();
this.title = in.readString();
in.readTypedArray(authors, Author.CREATOR);
this.isbn = in.readString();
this.price = in.readString();
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(this.id);
out.writeString(this.title);
out.writeArray(this.authors);
out.writeString(this.isbn);
out.writeString(this.price);
}
public int describeContents(){
return 0;
}
public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>() {
@Override
public Book createFromParcel(Parcel in) {
return new Book(in);
}
@Override
public Book[] newArray(int size) {
return new Book[size];
}
};
public Book(int id, String title, Author[] author, String isbn, String price) {
this.id = id;
this.title = title;
this.authors = author;
this.isbn = isbn;
this.price = price;
}
Book new_book = new Book(1, title, authors.parseAuthors(author), isbn, "35$");
Intent i = new Intent(this, BookActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("key", new_book);
i.putExtras(bundle);
startActivity(i);
Bundle bundle = getIntent().getExtras();
Book book = bundle.getParcelable("key");