android.os.Parcelable[] 无法转换为...错误
android.os.Parcelable[] cannot be cast to... Error
我正在为我的 class 做一个项目,但遇到了问题。当我添加一本书时出现以下错误有一个 parcelable authors class 它将是一个数组和一个 parcelable books class。作者被存储在可打包的书中:
错误:
java.lang.RuntimeException: Failure delivering result ResultInfo
{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity
{edu.stevens.cs522.bookstore/edu.stevens.cs522.bookstore.activities.BookStoreActivity}:
java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to
edu.stevens.cs522.bookstore.entities.Author[]
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
代码
作者:
package edu.stevens.cs522.bookstore.entities;
import android.os.Parcel;
import android.os.Parcelable;
public class Author implements Parcelable {
public String firstName = null;
public String middleInitial = null;
public String lastName = null;
public Author(String[] authorName) {
if (authorName.length == 1) {
this.lastName = authorName[0];
} else if (authorName.length == 2) {
this.firstName = authorName[0];
this.lastName = authorName[1];
} else if (authorName.length >= 3) {
this.firstName = authorName[0];
this.middleInitial = authorName[1];
this.lastName = authorName[2];
} else if (authorName.length == 0) {
//nothing needs to be done
}
}
public Author(String firstName, String middleInitial, String lastName) {
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
}
public Author(Parcel in) {
String[] data = new String[3];
in.readStringArray(data);
this.firstName = data[0];
this.middleInitial = data[1];
this.lastName = data[2];
}
public static final Parcelable.Creator<Author> CREATOR = new Parcelable.Creator<Author>() {
public Author createFromParcel(Parcel in) {
return new Author(in);
}
public Author[] newArray(int size) {
return new Author[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
String[] array = new String[3];
array[0] = this.firstName;
array[1] = this.middleInitial;
array[2] = this.lastName;
dest.writeStringArray(array);
}
}
密码本:
package edu.stevens.cs522.bookstore.entities;
import android.os.Parcel;
import android.os.Parcelable;
public class Book implements Parcelable {
// TODO Modify this to implement the Parcelable interface.
// TODO redefine toString() to display book title and price (why?).
public int id;
public String title;
public String isbn;
public String price;
public Author[] authors;
public Book(int id, String title, Author[] author, String isbn, String price) {
this.id = id;
this.title = title;
this.authors = new Author[author.length];
for (int i = 0; i < author.length; i++) {
authors[i] = author[i];
}
this.isbn = isbn;
this.price = price;
}
public Book(Parcel in) {
int intdata = in.readInt();
this.id = intdata;
String[] data = new String[3];
in.readStringArray(data);
this.title = data[0];
this.isbn = data[1];
this.price = data[2];
Author[] authorsT = (Author[]) in.readParcelableArray(Author.class.getClassLoader());
this.authors = new Author[authorsT.length];
for (int i = 0; i < authorsT.length; i++) {
authors[i] = authorsT[i];
}
}
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];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
String[] data = new String[3];
data[0] = title;
data[1] = isbn;
data[2] = price;
dest.writeStringArray(data);
if (authors == null) {
authors = new Author[1];
}
dest.writeParcelableArray(authors, flags);
}
@Override
public String toString() {
return isbn;
}
}
书籍和作者对象的创建位置:
public Book searchBook() {
/*
* Search for the specified book.
*/
// TODO Just build a Book object with the search criteria and return that.
EditText editAuthor = (EditText) findViewById(R.id.search_author);
String authorString = editAuthor.getText().toString();
String[] authors = authorString.split(", ");
Author[] authorsArray = new Author[authors.length];
for (int i = 0; i < authors.length; i++) {
authorsArray[i] = new Author(authors[i].split(" "));
}
EditText editTitle = (EditText) findViewById(R.id.search_title);
EditText editIsbn = (EditText) findViewById(R.id.search_isbn);
String title = editTitle.getText().toString();
String isbn = editIsbn.getText().toString();
Log.e(title, authorsArray[0].lastName);
Log.e("isbn:", isbn);
Book newBook = new Book(100, title, authorsArray, isbn, "10");
return newBook;
}
如何调用意图:
Book resultBook = searchBook();
Intent result = new Intent();
result.putExtra(BOOK_RESULT_KEY, resultBook);
setResult(RESULT_OK, result);
finish();
以及我的 onActivityResult 中的代码:
if(requestCode == ADD_REQUEST) {
Bundle data = intent.getExtras();
Book book = (Book)data.getParcelable(AddBookActivity.BOOK_RESULT_KEY);
Log.e ("BOOK_TITLE", book.title);
}
任何人都可以帮我弄清楚我做错了什么导致出现该错误吗?
java.lang.RuntimeException: Failure delivering result ResultInfo
{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity
{edu.stevens.cs522.bookstore/edu.stevens.cs522.bookstore.activities.BookStoreActivity}:
java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to
edu.stevens.cs522.bookstore.entities.Author[]
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
在 Book
class 中进行这些更改,以将 xxxParcelableArray()
方法替换为 xxxTypedArray()
方法:
Author[] authorsT = in.createTypedArray(Author.CREATOR);
//Author[] authorsT = (Author[]) in.readParcelableArray(Author.class.getClassLoader());
dest.writeTypedArray(authors, flags);
//dest.writeParcelableArray(authors, flags);
更改的原因在对 this question 的回答中进行了解释。
我正在为我的 class 做一个项目,但遇到了问题。当我添加一本书时出现以下错误有一个 parcelable authors class 它将是一个数组和一个 parcelable books class。作者被存储在可打包的书中:
错误:
java.lang.RuntimeException: Failure delivering result ResultInfo
{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity
{edu.stevens.cs522.bookstore/edu.stevens.cs522.bookstore.activities.BookStoreActivity}:
java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to
edu.stevens.cs522.bookstore.entities.Author[]
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
代码 作者:
package edu.stevens.cs522.bookstore.entities;
import android.os.Parcel;
import android.os.Parcelable;
public class Author implements Parcelable {
public String firstName = null;
public String middleInitial = null;
public String lastName = null;
public Author(String[] authorName) {
if (authorName.length == 1) {
this.lastName = authorName[0];
} else if (authorName.length == 2) {
this.firstName = authorName[0];
this.lastName = authorName[1];
} else if (authorName.length >= 3) {
this.firstName = authorName[0];
this.middleInitial = authorName[1];
this.lastName = authorName[2];
} else if (authorName.length == 0) {
//nothing needs to be done
}
}
public Author(String firstName, String middleInitial, String lastName) {
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
}
public Author(Parcel in) {
String[] data = new String[3];
in.readStringArray(data);
this.firstName = data[0];
this.middleInitial = data[1];
this.lastName = data[2];
}
public static final Parcelable.Creator<Author> CREATOR = new Parcelable.Creator<Author>() {
public Author createFromParcel(Parcel in) {
return new Author(in);
}
public Author[] newArray(int size) {
return new Author[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
String[] array = new String[3];
array[0] = this.firstName;
array[1] = this.middleInitial;
array[2] = this.lastName;
dest.writeStringArray(array);
}
}
密码本:
package edu.stevens.cs522.bookstore.entities;
import android.os.Parcel;
import android.os.Parcelable;
public class Book implements Parcelable {
// TODO Modify this to implement the Parcelable interface.
// TODO redefine toString() to display book title and price (why?).
public int id;
public String title;
public String isbn;
public String price;
public Author[] authors;
public Book(int id, String title, Author[] author, String isbn, String price) {
this.id = id;
this.title = title;
this.authors = new Author[author.length];
for (int i = 0; i < author.length; i++) {
authors[i] = author[i];
}
this.isbn = isbn;
this.price = price;
}
public Book(Parcel in) {
int intdata = in.readInt();
this.id = intdata;
String[] data = new String[3];
in.readStringArray(data);
this.title = data[0];
this.isbn = data[1];
this.price = data[2];
Author[] authorsT = (Author[]) in.readParcelableArray(Author.class.getClassLoader());
this.authors = new Author[authorsT.length];
for (int i = 0; i < authorsT.length; i++) {
authors[i] = authorsT[i];
}
}
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];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
String[] data = new String[3];
data[0] = title;
data[1] = isbn;
data[2] = price;
dest.writeStringArray(data);
if (authors == null) {
authors = new Author[1];
}
dest.writeParcelableArray(authors, flags);
}
@Override
public String toString() {
return isbn;
}
}
书籍和作者对象的创建位置:
public Book searchBook() {
/*
* Search for the specified book.
*/
// TODO Just build a Book object with the search criteria and return that.
EditText editAuthor = (EditText) findViewById(R.id.search_author);
String authorString = editAuthor.getText().toString();
String[] authors = authorString.split(", ");
Author[] authorsArray = new Author[authors.length];
for (int i = 0; i < authors.length; i++) {
authorsArray[i] = new Author(authors[i].split(" "));
}
EditText editTitle = (EditText) findViewById(R.id.search_title);
EditText editIsbn = (EditText) findViewById(R.id.search_isbn);
String title = editTitle.getText().toString();
String isbn = editIsbn.getText().toString();
Log.e(title, authorsArray[0].lastName);
Log.e("isbn:", isbn);
Book newBook = new Book(100, title, authorsArray, isbn, "10");
return newBook;
}
如何调用意图:
Book resultBook = searchBook();
Intent result = new Intent();
result.putExtra(BOOK_RESULT_KEY, resultBook);
setResult(RESULT_OK, result);
finish();
以及我的 onActivityResult 中的代码:
if(requestCode == ADD_REQUEST) {
Bundle data = intent.getExtras();
Book book = (Book)data.getParcelable(AddBookActivity.BOOK_RESULT_KEY);
Log.e ("BOOK_TITLE", book.title);
}
任何人都可以帮我弄清楚我做错了什么导致出现该错误吗?
java.lang.RuntimeException: Failure delivering result ResultInfo
{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity
{edu.stevens.cs522.bookstore/edu.stevens.cs522.bookstore.activities.BookStoreActivity}:
java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to
edu.stevens.cs522.bookstore.entities.Author[]
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
在 Book
class 中进行这些更改,以将 xxxParcelableArray()
方法替换为 xxxTypedArray()
方法:
Author[] authorsT = in.createTypedArray(Author.CREATOR);
//Author[] authorsT = (Author[]) in.readParcelableArray(Author.class.getClassLoader());
dest.writeTypedArray(authors, flags);
//dest.writeParcelableArray(authors, flags);
更改的原因在对 this question 的回答中进行了解释。