Android Parcelable Class - 内部 类 不能有静态声明
Android Parcelable Class - Inner classes cannot have static declarations
我一直在尝试创建一个 Android Parcelable 对象,但将 运行 保留在错误 "Inner classes cannot have static declarations" 中。作为参考,我一直在查看位于 here.
的官方 Android 教程
我目前的代码如下:
public class AppDetail implements Parcelable {
CharSequence label;
CharSequence name;
Drawable icon;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeArray(new Object[] { this.label, this.name, this.icon });
}
public static final Parcelable.Creator<AppDetail> CREATOR
= new Parcelable.Creator<AppDetail>() {
public AppDetail createFromParcel(Parcel in) {
return new AppDetail(in);
}
public AppDetail[] newArray(int size) {
return new AppDetail[size];
}
};
public AppDetail() {}
public AppDetail(Parcel in) {
Object[] data = in.readArray(AppDetail.class.getClassLoader());
this.label = (String)data[0];
this.name = (String)data[1];
this.icon = (Drawable)data[2];
}
}
我在网上发现其他人也遇到了类似的问题,并得出结论认为编译器不喜欢静态初始化程序块(而不是静态 class 本身)——我尝试遵循这个建议并像这样声明: public static Parcelable.Creator<AppDetail> CREATOR
并在其他地方初始化 - 但是我遇到了同样的错误。
我怎样才能得到这个/可以编译的东西?
Yes it is- should that matter?
是的,因为这就是您收到错误的原因。您的错误不是由于 Parcelable.Creator<AppDetail>
,而是由于 AppDetail
本身。您不能在内部 class.
上有 static
方法或数据成员,例如 CREATOR
这样做的最终效果是直接实现 Parcelable
的 classes 需要是 static
内部 classes(即 public static class AppDetail implements Parcelable
)或常规(非内部)Java classes.
基于此实现,只需将 AppDetail
设为 public static class
即可解决您的问题。
我一直在尝试创建一个 Android Parcelable 对象,但将 运行 保留在错误 "Inner classes cannot have static declarations" 中。作为参考,我一直在查看位于 here.
的官方 Android 教程我目前的代码如下:
public class AppDetail implements Parcelable {
CharSequence label;
CharSequence name;
Drawable icon;
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeArray(new Object[] { this.label, this.name, this.icon });
}
public static final Parcelable.Creator<AppDetail> CREATOR
= new Parcelable.Creator<AppDetail>() {
public AppDetail createFromParcel(Parcel in) {
return new AppDetail(in);
}
public AppDetail[] newArray(int size) {
return new AppDetail[size];
}
};
public AppDetail() {}
public AppDetail(Parcel in) {
Object[] data = in.readArray(AppDetail.class.getClassLoader());
this.label = (String)data[0];
this.name = (String)data[1];
this.icon = (Drawable)data[2];
}
}
我在网上发现其他人也遇到了类似的问题,并得出结论认为编译器不喜欢静态初始化程序块(而不是静态 class 本身)——我尝试遵循这个建议并像这样声明: public static Parcelable.Creator<AppDetail> CREATOR
并在其他地方初始化 - 但是我遇到了同样的错误。
我怎样才能得到这个/可以编译的东西?
Yes it is- should that matter?
是的,因为这就是您收到错误的原因。您的错误不是由于 Parcelable.Creator<AppDetail>
,而是由于 AppDetail
本身。您不能在内部 class.
static
方法或数据成员,例如 CREATOR
这样做的最终效果是直接实现 Parcelable
的 classes 需要是 static
内部 classes(即 public static class AppDetail implements Parcelable
)或常规(非内部)Java classes.
基于此实现,只需将 AppDetail
设为 public static class
即可解决您的问题。