Android ArrayList无法强制转换为Parcelable
我发现以下错误:Android ArrayList无法强制转换为Parcelable,android,classcastexception,parcelable,Android,Classcastexception,Parcelable,我发现以下错误: Key com..BookStatus应为Parcelable,但值为ArrayList ClassCastException:无法将ArrayList转换为Parcelable ->和therafter:无法启动活动-NullPointerException ArrayList确实具有BookStatus对象的值(listToPass) Key com.example.books.BookStatus应为Parcelable,但值为java.util.ArrayList。
- Key com..BookStatus应为Parcelable,但值为ArrayList
- ClassCastException:无法将ArrayList转换为Parcelable ->和therafter:无法启动活动-NullPointerException
- ArrayList确实具有BookStatus对象的值(listToPass)
- Key com.example.books.BookStatus应为Parcelable,但值为java.util.ArrayList。返回了默认值
- 尝试强制转换生成的内部异常:
- java.lang.ClassCastException:java.util.ArrayList不能强制转换为android.os.Parcelable
- 在com.example.books4locals.SearchActivity.onCreate(SearchActivity.java:46)->这是bundle.getparcelable方法
ArrayList<BookStatus> listToPass = new ArrayList<BookStatus>(); // fill with smthg
protected void onPostExecute(String result) {
if (listToPass != null) {
Intent i = new Intent(getApplicationContext(),
SearchActivity.class);
// ArrayList<BookStatus> bs = new ArrayList<BookStatus> (Arrays.asList(bArr));
i.putParcelableArrayListExtra("com.example.books.BookStatus", listToPass);
startActivity(i);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchactivity_main);
Bundle bundle = getIntent().getExtras();
ArrayList<BookStatus> bookStatus = bundle
.getParcelable("com.example.books.BookStatus");
public class BookStatus implements Parcelable {
public BookStatus() {
}
public BookStatus(Parcel in) {
this.bookId=in.readInt();
this.address=in.readString();
this.name=in.readString();
this.author=in.readString();
this.lenderUserId=in.readInt();
this.isbn=in.readInt();
this.postcode=in.readString();
this.town=in.readString();
this.telnumber=in.readString();
this.mail=in.readString();
this.duration=in.readInt();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.bookId);
dest.writeString(this.address);
dest.writeString(this.name);
dest.writeString(this.author);
dest.writeInt(this.lenderUserId);
dest.writeInt(this.isbn);
dest.writeString(this.postcode);
dest.writeString(this.town);
dest.writeString(this.telnumber);
dest.writeString(this.mail);
dest.writeInt(this.duration);
}
public void readFromParcel(Parcel parcel){
this.bookId = parcel.readInt();
this.address = parcel.readString();
this.name = parcel.readString();
this.author = parcel.readString();
this.lenderUserId = parcel.readInt();
this.isbn = parcel.readInt();
this.postcode = parcel.readString();
this.town = parcel.readString();
this.telnumber = parcel.readString();
this.mail = parcel.readString();
this.duration = parcel.readInt();
}
public static final Parcelable.Creator<BookStatus> CREATOR = new Parcelable.Creator<BookStatus>() {
public BookStatus createFromParcel(Parcel in) {
return new BookStatus(in);
}
public BookStatus[] newArray(int size) {
return new BookStatus[size];
}
};
ArrayList listToPass=new ArrayList();//填充smthg
受保护的void onPostExecute(字符串结果){
如果(listToPass!=null){
意图i=新意图(getApplicationContext(),
搜索活动(类);
//ArrayList bs=新的ArrayList(Arrays.asList(bArr));
i、 putParcelableArrayListExtra(“com.example.books.BookStatus”,listToPass);
星触觉(i);
SearchActivity.java
ArrayList<BookStatus> listToPass = new ArrayList<BookStatus>(); // fill with smthg
protected void onPostExecute(String result) {
if (listToPass != null) {
Intent i = new Intent(getApplicationContext(),
SearchActivity.class);
// ArrayList<BookStatus> bs = new ArrayList<BookStatus> (Arrays.asList(bArr));
i.putParcelableArrayListExtra("com.example.books.BookStatus", listToPass);
startActivity(i);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchactivity_main);
Bundle bundle = getIntent().getExtras();
ArrayList<BookStatus> bookStatus = bundle
.getParcelable("com.example.books.BookStatus");
public class BookStatus implements Parcelable {
public BookStatus() {
}
public BookStatus(Parcel in) {
this.bookId=in.readInt();
this.address=in.readString();
this.name=in.readString();
this.author=in.readString();
this.lenderUserId=in.readInt();
this.isbn=in.readInt();
this.postcode=in.readString();
this.town=in.readString();
this.telnumber=in.readString();
this.mail=in.readString();
this.duration=in.readInt();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.bookId);
dest.writeString(this.address);
dest.writeString(this.name);
dest.writeString(this.author);
dest.writeInt(this.lenderUserId);
dest.writeInt(this.isbn);
dest.writeString(this.postcode);
dest.writeString(this.town);
dest.writeString(this.telnumber);
dest.writeString(this.mail);
dest.writeInt(this.duration);
}
public void readFromParcel(Parcel parcel){
this.bookId = parcel.readInt();
this.address = parcel.readString();
this.name = parcel.readString();
this.author = parcel.readString();
this.lenderUserId = parcel.readInt();
this.isbn = parcel.readInt();
this.postcode = parcel.readString();
this.town = parcel.readString();
this.telnumber = parcel.readString();
this.mail = parcel.readString();
this.duration = parcel.readInt();
}
public static final Parcelable.Creator<BookStatus> CREATOR = new Parcelable.Creator<BookStatus>() {
public BookStatus createFromParcel(Parcel in) {
return new BookStatus(in);
}
public BookStatus[] newArray(int size) {
return new BookStatus[size];
}
};
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.searchactivity_main);
Bundle Bundle=getIntent().getExtras();
ArrayList bookStatus=bundle
.getParcelable(“com.example.books.BookStatus”);
BookStatus.java
ArrayList<BookStatus> listToPass = new ArrayList<BookStatus>(); // fill with smthg
protected void onPostExecute(String result) {
if (listToPass != null) {
Intent i = new Intent(getApplicationContext(),
SearchActivity.class);
// ArrayList<BookStatus> bs = new ArrayList<BookStatus> (Arrays.asList(bArr));
i.putParcelableArrayListExtra("com.example.books.BookStatus", listToPass);
startActivity(i);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.searchactivity_main);
Bundle bundle = getIntent().getExtras();
ArrayList<BookStatus> bookStatus = bundle
.getParcelable("com.example.books.BookStatus");
public class BookStatus implements Parcelable {
public BookStatus() {
}
public BookStatus(Parcel in) {
this.bookId=in.readInt();
this.address=in.readString();
this.name=in.readString();
this.author=in.readString();
this.lenderUserId=in.readInt();
this.isbn=in.readInt();
this.postcode=in.readString();
this.town=in.readString();
this.telnumber=in.readString();
this.mail=in.readString();
this.duration=in.readInt();
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.bookId);
dest.writeString(this.address);
dest.writeString(this.name);
dest.writeString(this.author);
dest.writeInt(this.lenderUserId);
dest.writeInt(this.isbn);
dest.writeString(this.postcode);
dest.writeString(this.town);
dest.writeString(this.telnumber);
dest.writeString(this.mail);
dest.writeInt(this.duration);
}
public void readFromParcel(Parcel parcel){
this.bookId = parcel.readInt();
this.address = parcel.readString();
this.name = parcel.readString();
this.author = parcel.readString();
this.lenderUserId = parcel.readInt();
this.isbn = parcel.readInt();
this.postcode = parcel.readString();
this.town = parcel.readString();
this.telnumber = parcel.readString();
this.mail = parcel.readString();
this.duration = parcel.readInt();
}
public static final Parcelable.Creator<BookStatus> CREATOR = new Parcelable.Creator<BookStatus>() {
public BookStatus createFromParcel(Parcel in) {
return new BookStatus(in);
}
public BookStatus[] newArray(int size) {
return new BookStatus[size];
}
};
公共类BookStatus实现可包裹{
公共图书状态(){
}
公共图书状态(包裹中){
this.bookId=in.readInt();
this.address=in.readString();
this.name=in.readString();
this.author=in.readString();
this.lenderUserId=in.readInt();
this.isbn=in.readInt();
this.postcode=in.readString();
this.town=in.readString();
this.telnumber=in.readString();
this.mail=in.readString();
this.duration=in.readInt();
}
@凌驾
公共int描述内容(){
//TODO自动生成的方法存根
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的写入(此bookId);
目的地记录(本地址);
目的地记录(此名称);
目的地书面限制(该作者);
目的地写入(this.lenderUserId);
目的地写入(本isbn);
目的地写网线(此邮政编码);
目的地(本镇);
目的地writeString(此电话号码);
目的地书面限制(本邮件);
目的写入(此持续时间);
}
公共无效readFromParcel(地块){
this.bookId=parcel.readInt();
this.address=parcel.readString();
this.name=parcel.readString();
this.author=parcel.readString();
this.lenderUserId=parcel.readInt();
this.isbn=parcel.readInt();
this.postcode=parcel.readString();
this.town=parcel.readString();
this.telnumber=parcel.readString();
this.mail=parcel.readString();
this.duration=parcel.readInt();
}
public static final Parcelable.Creator=新建Parcelable.Creator(){
公共图书状态createFromParcel(包裹中){
返回新的BookStatus(in);
}
public BookStatus[]新数组(整数大小){
返回新的BookStatus[大小];
}
};
尝试更改
i.putParcelableArrayListExtra("com.example.books.BookStatus", listToPass);
到
及
到
它应该是getParcelableArrayListExtra,而不是getParcelableArrayList。