gpt4 book ai didi

android - ArrayList 不能转换为 Parcelable

转载 作者:IT老高 更新时间:2023-10-28 23:31:15 25 4
gpt4 key购买 nike

我收到以下错误:

  • Key com..BookStatus 预期 Parcelable 但值是 ArrayList
  • ClassCastException: ArrayList 无法转换为 Parcelable-> and therafter: 无法启动 Activity - NullPointerException
  • ArrayList 确实有 BookStatus 对象 (listToPass) 的值

    • 键 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 方法

我能做什么?

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

关于android - ArrayList 不能转换为 Parcelable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23755301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com