gpt4 book ai didi

android - 可打包对象的数组列表不起作用

转载 作者:行者123 更新时间:2023-11-29 20:28:49 31 4
gpt4 key购买 nike

我创建了一个实现了 parcelable 接口(interface)的类。然后我创建了该类的数组列表并将 Intent 传递给另一个 Activity 。但是当 Activity 试图获取可打包数组列表时,发生了运行时异常

我的可打包类是

package com.myapps.sulu.firstapp;
import android.os.Parcel;
import android.os.Parcelable;
public class Details implements Parcelable {
int id = 0;
String name = "";
int number = 0;

public Details(int id, String name, int number) {
this.id = id;
this.name = name;
this.number = number;
}

public Details(Parcel in) {
this.id = in.readInt();
this.name = in.readString();
this.number = in.readInt();
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeInt(number);
dest.writeString(name);
}

public int getId() {
return id;
}

public String getName() {
return name;
}

public int getNumber() {
return number;
}


public static final Creator<Details> CREATOR = new Creator<Details>() {
@Override
public Details createFromParcel(Parcel in) {
return new Details(in);
}

@Override
public Details[] newArray(int size) {
return new Details[size];
}
};
}

我在这里创建数组列表

Cursor cursor = writableDatabase.query(TestContracts.TestTable.TABLE_NAME, projection,
null, null, null, null, null);
cursor.moveToFirst();
ArrayList<Details> allDetailses = new ArrayList<Details>();
do {
int id = cursor.getInt(cursor.getColumnIndex(TestContracts.TestTable.ID));
int phone_number = cursor.getInt(cursor.getColumnIndex(TestContracts.TestTable.NUMBER));
String name = cursor.getString(cursor.getColumnIndex(TestContracts.TestTable.NAME));
Details details = new Details(id, name, phone_number);
Log.d("MSG", name + " " + id + " " + phone_number);
allDetailses.add(details);
} while (cursor.moveToNext());

Intent intent = new Intent(DbActivity.this, DetailsActivity.class);
intent.putParcelableArrayListExtra("values", allDetailses);
startActivity(intent);

我在如下所示的 getParcalableArrayList 行中遇到了异常

 Intent intent = getIntent();
values = intent.getParcelableArrayListExtra("values");
showRows();

异常(exception)是

08-27 16:48:39.507    7277-7277/com.myapps.sulu.firstapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapps.sulu.firstapp/com.myapps.sulu.firstapp.DetailsActivity}: java.lang.RuntimeException: Parcel android.os.Parcel@42a11020: Unmarshalling unknown type code 7536737 at offset 124
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
at android.app.ActivityThread.access$700(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@42a11020: Unmarshalling unknown type code 7536737 at offset 124
at android.os.Parcel.readValue(Parcel.java:2032)
at android.os.Parcel.readListInternal(Parcel.java:2235)
at android.os.Parcel.readArrayList(Parcel.java:1655)
at android.os.Parcel.readValue(Parcel.java:1986)
at android.os.Parcel.readMapInternal(Parcel.java:2226)
at android.os.Bundle.unparcel(Bundle.java:223)
at android.os.Bundle.getParcelableArrayList(Bundle.java:1217)
at android.content.Intent.getParcelableArrayListExtra(Intent.java:4837)
at com.myapps.sulu.firstapp.DetailsActivity.onCreate(DetailsActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5326)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309) 
at android.app.ActivityThread.access$700(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

问题是您按以下顺序填写包裹:

  • id
  • 编号
  • 姓名

当您尝试从包裹中重新创建对象时,您可以使用以下顺序:

  • id
  • 姓名
  • 编号

重要的是,当您将您的值放入一个 Parcel 时,您按照与从该 Parcel 读取它们时相同的顺序进行操作。因此,您必须在从 Creator 调用的构造函数或 writeToParcel 方法中切换 namenumber

关于android - 可打包对象的数组列表不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32248886/

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