0

我有 Arraylist 的通用类型。我用过Parcelable但它总是抛出错误我用过以下代码

内容

public class Content implements Parcelable {
    private String pc, cc, sc, uId, name, title, image, image2, url2g, url3g,
            urlHd, rating, views, smallDesc, longDesc, dvrId, id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUID() {
        return uId;
    }

    public String getPC() {
        return pc;
    }

    public String getDVRID() {
        return dvrId;
    }

    public void setDVRID(String dVRID) {
        dvrId = dVRID;
    }

    public String getIMAGE2() {
        return image2;
    }

    public void setIMAGE2(String iMAGE2) {
        image2 = iMAGE2;
    }

    public void setPC(String pC) {
        pc = pC;
    }

    public String getCC() {
        return cc;
    }

    public void setCC(String cC) {
        cc = cC;
    }

    public String getSC() {
        return sc;
    }

    public void setSC(String sC) {
        sc = sC;
    }

    public void setUID(String uID) {
        uId = uID;
    }

    public String getNAME() {
        return name;
    }

    public void setNAME(String nAME) {
        name = nAME;
    }

    public String getTITLE() {
        return title;
    }

    public void setTITLE(String tITLE) {
        title = tITLE;
    }

    public String getIMAGE() {
        return image;
    }

    public void setIMAGE(String iMAGE) {
        image = iMAGE;
    }

    public String getURL2G() {
        return url2g;
    }

    public void setURL2G(String uRL2G) {
        url2g = uRL2G;
    }

    public String getURL3G() {
        return url3g;
    }

    public void setURL3G(String uRL3G) {
        url3g = uRL3G;
    }

    public String getURLHD() {
        return urlHd;
    }

    public void setURLHD(String uRLHD) {
        urlHd = uRLHD;
    }

    public String getRATING() {
        return rating;
    }

    public void setRATING(String rATING) {
        rating = rATING;
    }

    public String getVIEWS() {
        return views;
    }

    public void setVIEWS(String vIEWS) {
        views = vIEWS;
    }

    public String getSMALLDESC() {
        return smallDesc;
    }

    public void setSMALLDESC(String sMALLDESC) {
        smallDesc = sMALLDESC;
    }

    public String getLONGDESC() {
        return longDesc;
    }

    public void setLONGDESC(String lONGDESC) {
        longDesc = lONGDESC;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel out, int arg1) {
        // TODO Auto-generated method stub
        out.writeString(pc);
        out.writeString(sc);
        out.writeString(cc);
    }

    public static final Parcelable.Creator<Content> CREATOR = new Parcelable.Creator<Content>() {
        public Content createFromParcel(Parcel in) {
            return new Content();
        }

        public Content[] newArray(int size) {
            return new Content[size];
        }
    };
}

并使用以下代码调用传递值

Intent details = new Intent(context, B.class);
        details.putExtra("category", LIVE);
        details.putParcelableArrayListExtra("arrayList",
                showcase);
        startActivity(details);

获得价值

category = getIntent().getExtras().getInt("category");
        content = getIntent().getParcelableArrayListExtra("arrayList");

日志猫

FATAL EXCEPTION: main
E/AndroidRuntime( 4666): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zengamedia.zengatv/com.zengamedia.zengatv.Details}: java.lang.RuntimeException: Parcel android.os.Parcel@40f6de20: Unmarshalling unknown type code 3014771 at offset 112
E/AndroidRuntime( 4666):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
E/AndroidRuntime( 4666):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
E/AndroidRuntime( 4666):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
E/AndroidRuntime( 4666):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
E/AndroidRuntime( 4666):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 4666):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 4666):    at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime( 4666):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 4666):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 4666):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime( 4666):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime( 4666):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 4666): Caused by: java.lang.RuntimeException: Parcel android.os.Parcel@40f6de20: Unmarshalling unknown type code 3014771 at offset 112
E/AndroidRuntime( 4666):    at android.os.Parcel.readValue(Parcel.java:1921)
E/AndroidRuntime( 4666):    at android.os.Parcel.readMapInternal(Parcel.java:2094)
E/AndroidRuntime( 4666):    at android.os.Parcel.readHashMap(Parcel.java:1439)
E/AndroidRuntime( 4666):    at android.os.Parcel.readValue(Parcel.java:1851)
E/AndroidRuntime( 4666):    at android.os.Parcel.readListInternal(Parcel.java:2103)
E/AndroidRuntime( 4666):    at android.os.Parcel.readArrayList(Parcel.java:1544)
E/AndroidRuntime( 4666):    at android.os.Parcel.readValue(Parcel.java:1875)
E/AndroidRuntime( 4666):    at android.os.Parcel.readMapInternal(Parcel.java:2094)
E/AndroidRuntime( 4666):    at android.os.Bundle.unparcel(Bundle.java:223)
E/AndroidRuntime( 4666):    at android.os.Bundle.getParcelableArrayList(Bundle.java:1202)
E/AndroidRuntime( 4666):    at android.content.Intent.getParcelableArrayListExtra(Intent.java:4005)
E/AndroidRuntime( 4666):    at com.zengamedia.zengatv.Details.onCreate(Details.java:50)
E/AndroidRuntime( 4666):    at android.app.Activity.performCreate(Activity.java:4492)
E/AndroidRuntime( 4666):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
E/AndroidRuntime( 4666):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
E/AndroidRuntime( 4666):    ... 11 more

请建议我在哪里做错了

4

4 回答 4

2

您的 Parcelable 实现根本不正确。

看看这个很棒的工具:parcelabler

只要您的类中没有任何更复杂的类型,它将帮助您为它键入所有烦人的代码。只需将整个班级复制粘贴到其中即可。

似乎这个工具在间距和创建多个字符串方面有点笨拙。这应该有效;

public class Content implements Parcelable {

private String pc, cc, sc, uId, name, title, image, image2, url2g, url3g,
        urlHd, rating, views, smallDesc, longDesc, dvrId, id;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getUID() {
    return uId;
}

public String getPC() {
    return pc;
}

public String getDVRID() {
    return dvrId;
}

public void setDVRID(String dVRID) {
    dvrId = dVRID;
}

public String getIMAGE2() {
    return image2;
}

public void setIMAGE2(String iMAGE2) {
    image2 = iMAGE2;
}

public void setPC(String pC) {
    pc = pC;
}

public String getCC() {
    return cc;
}

public void setCC(String cC) {
    cc = cC;
}

public String getSC() {
    return sc;
}

public void setSC(String sC) {
    sc = sC;
}

public void setUID(String uID) {
    uId = uID;
}

public String getNAME() {
    return name;
}

public void setNAME(String nAME) {
    name = nAME;
}

public String getTITLE() {
    return title;
}

public void setTITLE(String tITLE) {
    title = tITLE;
}

public String getIMAGE() {
    return image;
}

public void setIMAGE(String iMAGE) {
    image = iMAGE;
}

public String getURL2G() {
    return url2g;
}

public void setURL2G(String uRL2G) {
    url2g = uRL2G;
}

public String getURL3G() {
    return url3g;
}

public void setURL3G(String uRL3G) {
    url3g = uRL3G;
}

public String getURLHD() {
    return urlHd;
}

public void setURLHD(String uRLHD) {
    urlHd = uRLHD;
}

public String getRATING() {
    return rating;
}

public void setRATING(String rATING) {
    rating = rATING;
}

public String getVIEWS() {
    return views;
}

public void setVIEWS(String vIEWS) {
    views = vIEWS;
}

public String getSMALLDESC() {
    return smallDesc;
}

public void setSMALLDESC(String sMALLDESC) {
    smallDesc = sMALLDESC;
}

public String getLONGDESC() {
    return longDesc;
}

public void setLONGDESC(String lONGDESC) {
    longDesc = lONGDESC;
}

@Override
public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
}

public static final Parcelable.Creator<Content> CREATOR = new Parcelable.Creator<Content>() {
    public Content createFromParcel(Parcel in) {
        return new Content();
    }

    public Content[] newArray(int size) {
        return new Content[size];
    }
};

protected Content(Parcel in) {
    pc = in.readString();
    cc = in.readString();
    sc = in.readString();
    uId = in.readString();
    name = in.readString();
    title = in.readString();
    image = in.readString();
    image2 = in.readString();
    url2g = in.readString();
    url3g = in.readString();
    urlHd = in.readString();
    rating = in.readString();
    views = in.readString();
    smallDesc = in.readString();
    longDesc = in.readString();
    dvrId = in.readString();
    id = in.readString();
}

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(pc);
    dest.writeString(cc);
    dest.writeString(sc);
    dest.writeString(uId);
    dest.writeString(name);
    dest.writeString(title);
    dest.writeString(image);
    dest.writeString(image2);
    dest.writeString(url2g);
    dest.writeString(url3g);
    dest.writeString(urlHd);
    dest.writeString(rating);
    dest.writeString(views);
    dest.writeString(smallDesc);
    dest.writeString(longDesc);
    dest.writeString(dvrId);
    dest.writeString(id);
}

public static final Parcelable.Creator<Content> CREATOR = new Parcelable.Creator<Content>() {
    public Content createFromParcel(Parcel in) {
        return new Content(in);
    }

    public Content[] newArray(int size) {
        return new Content[size];
    }
};

}

于 2013-05-14T09:43:45.483 回答
1
Define your List static and access any where in your application.
于 2013-05-14T09:40:24.227 回答
0

这是一个ArrayList<Object>如何通过意图发送的示例。

于 2013-05-14T09:45:44.990 回答
0

看看下面的例子,我已经介绍了String,Integer,double数据类型

import android.os.Parcel;
import android.os.Parcelable;

public class Poi implements Parcelable{
    private Integer poiId;
    private String poiLabel;
    private String poiInfo;
    private String poiImageName;
    private String poiHistoricalImageName;
    private String inPaths;
    private String poiType;
    private double longitude;
    private double latitude;


public Poi() {
}

public Poi(Integer poiId, String poiLabel, String poiInfo,
        String poiImageName,String poiHistoricalImageName, String inPaths, String poiType,
        double longitude, double latitude) {
    super();
    this.poiId = poiId;
    this.poiLabel = poiLabel;
    this.poiInfo = poiInfo;
    this.poiImageName = poiImageName;
    this.poiHistoricalImageName = poiHistoricalImageName;
    this.inPaths = inPaths;
    this.poiType = poiType;
    this.longitude = longitude;
    this.latitude = latitude;
}

public Integer getPoiId() {
    return poiId;
}

public void setPoiId(Integer poiId) {
    this.poiId = poiId;
}

public String getPoiLabel() {
    return poiLabel;
}

public void setPoiLabel(String poiLabel) {
    this.poiLabel = poiLabel;
}

public String getPoiInfo() {
    return poiInfo;
}

public void setPoiInfo(String poiInfo) {
    this.poiInfo = poiInfo;
}

public String getPoiImageName() {
    return poiImageName;
}

public void setPoiImageName(String poiImageName) {
    this.poiImageName = poiImageName;
}

public String getPoiHistoricalImageName() {
    return poiHistoricalImageName;
}

public void setPoiHistoricalImageName(String poiHistoricalImageName) {
    this.poiHistoricalImageName = poiHistoricalImageName;
}

public String getInPaths() {
    return inPaths;
}

public void setInPaths(String inPaths) {
    this.inPaths = inPaths;
}

public String getPoiType() {
    return poiType;
}

public void setPoiType(String poiType) {
    this.poiType = poiType;
}

public double getLongitude() {
    return longitude;
}

public void setLongitude(double longitude) {
    this.longitude = longitude;
}

public double getLatitude() {
    return latitude;
}

public void setLatitude(double latitude) {
    this.latitude = latitude;
}


 @Override
public String toString() {
    return "Poi [poiId=" + poiId + ", poiLabel=" + poiLabel + ", poiInfo="
            + poiInfo + ", poiImageName=" + poiImageName
            + ", poiHistoricalImageName=" + poiHistoricalImageName
            + ", inPaths=" + inPaths + ", poiType=" + poiType
            + ", longitude=" + longitude + ", latitude=" + latitude + "]";
}


//parcelable stuff

public Poi (Parcel in) {
     readFromParcel(in);
 }

private void readFromParcel(Parcel in) {

    poiId = in.readInt();
    poiLabel = in.readString();
    poiInfo = in.readString();
    poiImageName = in.readString();
    poiHistoricalImageName = in.readString();
    inPaths = in.readString();
    poiType = in.readString();
    longitude = in.readDouble();
    latitude = in.readDouble();

}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public Poi createFromParcel(Parcel in)
    {
        return new Poi(in);
    }

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


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

@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeInt(poiId);
    dest.writeString(poiLabel);
    dest.writeString(poiInfo);
    dest.writeString(poiImageName);
    dest.writeString(poiHistoricalImageName);
    dest.writeString(inPaths);
    dest.writeString(poiType);
    dest.writeDouble(longitude);
    dest.writeDouble(latitude);
}

}

我希望它会有所帮助!

于 2013-05-14T09:50:42.560 回答