我正在开发一个 Android 应用程序,我正在尝试使用 Parcelable 传递信息。这就是我所拥有的。
import android.os.Parcel;
import android.os.Parcelable;
abstract class Role implements Parcelable {
private String name;
private String image;
public Role() {
}
public Role(Parcel read) {
name = read.readString();
image = read.readString();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String toString() {
return this.name;
}
public static final Parcelable.Creator<Role> CREATOR =
new Parcelable.Creator<Role>() {
public Role createFromParcel(Parcel source) {
return new Role(source);
}
public Role[] newArray(int size) {
return new Role[size];
}
};
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(name);
dest.writeString(image);
}
}
但是,当我尝试编译时出现错误(我放置注释的位置)
Cannot instantiate the Type Role
对此有什么想法吗?
最好的问候
最佳答案
我自己没有在抽象类中使用过parcelable,不过应该没问题。您可能需要检查 here或更一般地说 here
我有一个非常相似的类(两个字符串),但它是一个公共(public)静态类。 我在构造函数中对我的字符串成员执行 new()。
关于java - Android Parcelable 无法实例化类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12447250/