gpt4 book ai didi

android - 尝试对自定义对象数组执行 readParcelable 时发生 ClassCastException

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:28 32 4
gpt4 key购买 nike

我正在尝试实现 Parcelable 而不是 Serializble,因为它应该更有效率。从我的 MainActivity 我想将 Message 对象传递给另一个 Activity ,我的类 Message 包含其他类。如果我克服了这个错误,我肯定会遇到更多错误,但现在当我尝试运行我的应用程序时,我得到了

这是我的消息类:

import java.util.ArrayList;

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

public class Message implements Parcelable {

int id;
int space_id;
String text;
String html;
String created_at;
int[] comments;
boolean is_liked;
int[] liked_by;
int[] starred_by;
boolean is_starred;
boolean is_followed;
int[] followed_by;
ArrayList<AttachmentsData> attachments;
String url;
int[] tags;
boolean is_system;

public User created_by;
public Permissions persmissions;
public Embed embed;
public Tokens[] tokens;


/** Parcelable **/
public Message(Parcel in) {
readFromParcel(in);
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeInt(space_id);
dest.writeString(text);
dest.writeString(html);
dest.writeString(created_at);
dest.writeIntArray(comments);
dest.writeInt(is_liked ? 1 : 0);
dest.writeIntArray(liked_by);
dest.writeIntArray(starred_by);
dest.writeInt(is_starred ? 1 : 0);
dest.writeInt(is_followed ? 1 : 0);
dest.writeIntArray(followed_by);
dest.writeList(attachments);
dest.writeString(url);
dest.writeIntArray(tags);
dest.writeInt(is_system ? 1 : 0);

dest.writeParcelable(created_by, flags);
dest.writeParcelable(persmissions, flags);
dest.writeParcelable(embed, flags);
dest.writeParcelableArray(tokens, flags);
}

private void readFromParcel(Parcel in) {
id = in.readInt();
space_id = in.readInt();
text = in.readString();
html = in.readString();
created_at = in.readString();
//in.readIntArray(comments);
comments = in.createIntArray();
is_liked = in.readInt() == 0;
//in.readIntArray(liked_by);
liked_by = in.createIntArray();
//in.readIntArray(starred_by);
starred_by = in.createIntArray();
is_starred = in.readInt() == 0;
is_followed = in.readInt() == 0;
//in.readIntArray(followed_by);
followed_by = in.createIntArray();
in.readList(attachments, AttachmentsData.class.getClassLoader());
url = in.readString();
//in.readIntArray(tags);
tags = in.createIntArray();
is_system = in.readInt() == 0;

created_by = in.readParcelable(User.class.getClassLoader());
persmissions = in.readParcelable(Permissions.class.getClassLoader());
embed = in.readParcelable(Embed.class.getClassLoader());
tokens = in.readParcelable(Tokens.class.getClassLoader();
}

public static final Parcelable.Creator<Message> CREATOR = new Parcelable.Creator<Message>() {

public Message createFromParcel(Parcel in) {
return new Message(in);
}

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



/** Getters and setters, I'll spare you these **/

}

这是我得到的错误:

FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.incentive/com.example.incentive.Comments}: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.example.incentive.messagemodel.Tokens[] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) at android.app.ActivityThread.access$600(ActivityThread.java:127) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4441) 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:784) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.example.incentive.messagemodel.Tokens[] at com.example.incentive.messagemodel.Message.readFromParcel(Message.java:94) at com.example.incentive.messagemodel.Message.(Message.java:35) at com.example.incentive.messagemodel.Message$1.createFromParcel(Message.java:100) at com.example.incentive.messagemodel.Message$1.createFromParcel(Message.java:1) at android.os.Parcel.readParcelable(Parcel.java:1992) at android.os.Parcel.readValue(Parcel.java:1854) at android.os.Parcel.readMapInternal(Parcel.java:2094) at android.os.Bundle.unparcel(Bundle.java:223) at android.os.Bundle.getParcelable(Bundle.java:1158) at com.example.incentive.Comments.onCreate(Comments.java:67) at android.app.Activity.performCreate(Activity.java:4465) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) ... 11 more

我认为现在相关的是:

Caused by: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.example.incentive.messagemodel.Tokens[] at com.example.incentive.messagemodel.Message.readFromParcel(Message.java:94)

所以我可能做错了这一件事: tokens = in.readParcelable(Tokens.class.getClassLoader();

这是我的 token 类:

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

public class Tokens implements Parcelable {

String type;
String value;
int start;
int end;


/** Parcelable related **/
@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(type);
dest.writeString(value);
dest.writeInt(start);
dest.writeInt(end);
}

private Tokens(Parcel in) {
type = in.readString();
value = in.readString();
start = in.readInt();
end = in.readInt();
}

public static final Parcelable.Creator<Tokens> CREATOR = new Parcelable.Creator<Tokens>() {

public Tokens createFromParcel(Parcel in) {
return new Tokens(in);
}

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


/** Getters and setters **/
}

我搜索了整个下午,但找不到正确的方法。有人知道吗?

最佳答案

使用 writeTypedArray 和 readTypedArray 读取数组并将其写入包裹

阅读

tokens = in.createTypedArray(Tokens.CREATOR);

in.writeTypedArray(tokens, flags);

关于android - 尝试对自定义对象数组执行 readParcelable 时发生 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20353585/

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