可分解对象的Arraylist不工作

时间:2015-08-27 12:04:01

标签: android parcelable

我创建了一个实现parcelable接口的类。然后我创建了该类的数组列表,并将意图传递给另一个活动。但是当活动试图获取可分解的数组列表时,运行时异常正在开心

我的parcelable类是

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();

例外是

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)

2 个答案:

答案 0 :(得分:2)

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

  • id
  • number
  • name

当您尝试从宗地重新创建对象时,请使用此顺序:

  • id
  • name
  • number

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

答案 1 :(得分:0)

您需要在readFromParcel课程中实施Details方法。