Android 意图仅在startAtivityForResult时返回部分对象
我得给书和作者上课。和book类包含一个author数组。我使用startActivityForResult从主活动开始添加书本活动。在子活动中,我构建了一个author数组,并确保它包含刚刚初始化的author对象。然后把它传回去。代码如下:Android 意图仅在startAtivityForResult时返回部分对象,android,android-intent,Android,Android Intent,我得给书和作者上课。和book类包含一个author数组。我使用startActivityForResult从主活动开始添加书本活动。在子活动中,我构建了一个author数组,并确保它包含刚刚初始化的author对象。然后把它传回去。代码如下: Log.d("djdjdjjdjdjdjdjdjdj", authors[0].firstName);//could display the firstName; intent.putExtra("book", new Book(title, auth
Log.d("djdjdjjdjdjdjdjdjdj", authors[0].firstName);//could display the firstName;
intent.putExtra("book", new Book(title, authors, isbn, price));
setResult(Activity.RESULT_OK,intent);
finish();
在这种情况下,LogCat可以显示第一个名称
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if(requestCode == ADD_REQUEST) {
if (resultCode == RESULT_OK) {
Bundle data = intent.getExtras();
Book book = (Book) data.getParcelable("book");
Log.d("JJJJJJJJJJJJ", book.getTitle());//could display the title;
Log.d("UUUUUUUUUUUU", book.getAuthors()[0].firstName);//NullPointerException: Attempt to read from null array;
shoppingCart.add(book);
viewHolderAdapter.notifyDataSetChanged();
}
}
}
当返回到主活动并提取图书对象时,它包含标题但不包含作者数组。为什么?
这是图书课:
public class Book implements Parcelable{
public String title;
public Author[] authors;
public String isbn;
public String price;
public Book(String title, Author[] authors, String isbn, String price) {
this.title = title;
this.authors = authors;
this.isbn = isbn;
this.price = price;
}
public String getTitle()
{
return title;
}
public Author[] getAuthors()
{
return authors;
}
public String getIsbn()
{
return isbn;
}
public String getPrice()
{
return price;
}
public int describeContents(){
return 0;
}
public Book(Parcel in)
{
Parcelable[] parcelableArray =
in.readParcelableArray(Author.class.getClassLoader());
Author[] authors = null;
if (parcelableArray != null) {
authors = Arrays.copyOf(parcelableArray, parcelableArray.length, Author[].class);
}
this.title = in.readString();
this.isbn = in.readString();
this.price = in.readString();
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelableArray(authors, flags);
dest.writeString(title);
dest.writeString(isbn);
dest.writeString(price);
}
private void readFromParcel(Parcel in) {
authors = in.createTypedArray(Author.CREATOR);
title = in.readString();
isbn = in.readString();
price = in.readString();
}
public static final Parcelable.Creator<Book> CREATOR
= new Parcelable.Creator<Book>() {
public Book createFromParcel(Parcel in) {
return new Book(in);
}
public Book[] newArray(int size) {
return new Book[size];
}
};
}
提前感谢。请看一看《类构造函数:
Parcelable[] parcelableArray =
in.readParcelableArray(Author.class.getClassLoader());
Author[] authors = null;
if (parcelableArray != null) {
authors = Arrays.copyOf(parcelableArray, parcelableArray.length, Author[].class);
}
您已经创建了authors变量,为其分配了null,并将解析后的数组复制到该变量中。但您应该使用assing Book类变量
只需删除字符串Author[]authors=null;您的代码也会很好地工作。我设法用另一个解决方案回答我自己的问题。。。 我只是替换:
Parcelable[] parcelableArray =
in.readParcelableArray(Author.class.getClassLoader());
Author[] authors = null;
if (parcelableArray != null) {
authors = Arrays.copyOf(parcelableArray, parcelableArray.length, Author[].class);
}
与:
并替换:
dest.writeParcelableArray(authors, flags);
与:
把你的书贴在课堂上
dest.writeParcelableArray(authors, flags);
dest.writeTypedArray(authors, flags);