ArrayList无法强制转换为Parcelable [英] ArrayList cannot be cast to Parcelable
本文介绍了ArrayList无法强制转换为Parcelable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到以下错误:
- Key com..BookStatus预期为可拆分,但值是ArrayList
- ClassCastException:ArrayList不能转换为可包裹的
->及其后:无法启动活动-NullPointerException -
ArrayList确实具有BookStatus对象的值(listToPass )
- Key com..BookStatus expected Parcelable but value was a ArrayList
- ClassCastException: ArrayList cannot be cast to Parcelable -> and therafter: Unable to start Activity - NullPointerException
the ArrayList do have values of BookStatus Objects (listToPass)
- 键com.example.books.BookStatus期望可拆分,但值是java.util.ArrayList。返回默认值
- 尝试强制转换生成的内部异常:
- java.lang.ClassCastException:java.util.ArrayList无法强制转换为android .os.Parcelable
- 位于com.example.books4locals.SearchActivity.onCreate(SearchActivity.java:46)->这是bundle.getparcelable方法
- Key com.example.books.BookStatus expected Parcelable but value was a java.util.ArrayList. The default value was returned
- Attempt to cast generated internal exception:
- java.lang.ClassCastException: java.util.ArrayList cannot be cast to android.os.Parcelable
- at com.example.books4locals.SearchActivity.onCreate(SearchActivity.java:46) -> which is the bundle.getparcelable method
我该怎么办?
HomeActivity .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);
SearchActivity.java
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");
BookStatus.java
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];
}
};
推荐答案
尝试更改
i.putParcelableArrayListExtra("com.example.books.BookStatus", listToPass);
到
i.putExtra("com.example.books.BookStatus", listToPass);
和
bundle.getParcelable
至
bundle.getParcelableArrayList
这篇关于ArrayList无法强制转换为Parcelable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文