我正在尝试实现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:无法启动 活动 ComponentInfo {com.example.incentive / com.example.incentive.Comments}: java.lang.ClassCastException:无法转换android.os.Parcelable [] to com.example.incentive.messagemodel.Tokens [] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992) 在android.app.ActivityThread.access $ 600(ActivityThread.java:127)at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1158) 在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) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)at dalvik.system.NativeStart.main(Native方法)引起: java.lang.ClassCastException:无法转换android.os.Parcelable [] to com.example.incentive.messagemodel.Tokens [] at com.example.incentive.messagemodel.Message.readFromParcel(Message.java:94) 在com.example.incentive.messagemodel.Message。(Message.java:35) 在 com.example.incentive.messagemodel.Message $ 1.createFromParcel(Message.java:100) 在 com.example.incentive.messagemodel.Message $ 1.createFromParcel(Message.java:1) 在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) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931) ......还有11个
我现在认为相关的是:
引起:java.lang.ClassCastException:android.os.Parcelable [] 无法转换为com.example.incentive.messagemodel.Tokens [] at com.example.incentive.messagemodel.Message.readFromParcel(Message.java:94)
所以我可能做错了一件事: tokens = in.readParcelable(Tokens.class.getClassLoader();
这是我的Tokens-class:
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 **/
}
我整个下午都在搜索,我看不清楚如何正确地做到这一点。有谁知道吗?
答案 0 :(得分:19)
使用writeTypedArray和readTypedArray读取并将数组写入parcel
读
tokens = in.createTypedArray(Tokens.CREATOR);
写
in.writeTypedArray(tokens, flags);
答案 1 :(得分:0)
试试这个......
替换
tokens = in.readParcelable(Tokens.class.getClassLoader();
与
tokens = in.readParcelableArray(Tokens.class.getClassLoader());
您正在编写Parcelable Array,但将其读作Parcelable