使用可分割对象时无法获取列表元素
我有一个具有服务和活动的应用程序。我的基类和列表类正在实现 Parcelable。列表类使用 writeTypedList()/readTypedList() 来打包自定义列表。但是,在检索列表时,我获得了正确的元素数量,但在尝试访问列表第一个元素之外的任何元素时,出现空指针异常。当执行 writeToParcel() 时,我可以看到添加了 2 个具有正确数据的元素,但是当执行 readFromParcel() 时,仅显示第一个元素,第二个元素为 null。
任何帮助将不胜感激!
这是我的列表容器类:
public class WfdDeviceList implements Parcelable {
private static final String TAG = "WfdDeviceList";
public List<WfdDevice> devList;
public WfdDeviceList() { devList = new ArrayList<WfdDevice>(); }
public WfdDeviceList(Parcel parcel) {
this();
readFromParcel(parcel);
}
public void clear() {
devList.clear();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
Log.d(TAG, "writeToParcel(): " + devList.size());
Log.d(TAG, "flags: " + flags);
for (WfdDevice device : devList) {
Log.d(TAG, "Writing: " + device.name);
}
parcel.writeTypedList(devList);
}
public void readFromParcel(Parcel parcel) {
Log.d(TAG, "readFromParcel()");
parcel.readTypedList(devList, WfdDevice.CREATOR);
Log.d(TAG, "Size: " + devList.size());
WfdDevice device = devList.get(0);
Log.d(TAG, "Device1: " + device.name);
}
public static final Parcelable.Creator<WfdDeviceList> CREATOR = new Parcelable
.Creator<WfdDeviceList>() {
public WfdDeviceList createFromParcel(Parcel in) {
return new WfdDeviceList(in);
}
public WfdDeviceList[] newArray(int size) {
return new WfdDeviceList[size];
}
};
}
public class WfdDevice implements Parcelable {
public String macAddress;
public String name;
public WfdDeviceType type;
public String ipAddr;
public String pin;
public boolean isConnected;
public WfdDevice() {}
public WfdDevice(Parcel parcel) {
readFromParcel(parcel);
}
@Override
public int describeContents() {
return 0;
}
WfdDeviceType getType(int type) {
switch(type) {
case 1:
return WfdDeviceType.source;
case 2:
return WfdDeviceType.primarySink;
case 3:
return WfdDeviceType.secondarySink;
case 4:
return WfdDeviceType.sourcePrimarySink;
default:
case 0:
return WfdDeviceType.unknown;
}
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(this.macAddress);
parcel.writeString(this.name);
parcel.writeInt(type.ordinal());
parcel.writeString(this.ipAddr);
parcel.writeString(this.pin);
parcel.writeInt(this.isConnected == true ? 1 : 0);
}
public void readFromParcel(Parcel parcel) {
this.macAddress = parcel.readString();
this.name = parcel.readString();
this.type = getType(parcel.readInt());
this.ipAddr = parcel.readString();
this.pin = parcel.readString();
this.isConnected = (parcel.readInt() == 1 ? true : false);
}
public static final Parcelable.Creator<WfdDevice> CREATOR = new Parcelable
.Creator<WfdDevice>() {
public WfdDevice createFromParcel(Parcel in) {
return new WfdDevice(in);
}
public WfdDevice[] newArray(int size) {
return new WfdDevice[size];
}
};
}
I have an application that has a service and an activity. My base class and list class are implementing parcelable. And the list class uses writeTypedList()/readTypedList() to parcel a custom list. However when retrieving the list I'm getting the correct number of elements, but am getting a null pointer exception when trying to access any elements besides the first element of the list. When doing a writeToParcel() I can see that 2 elements with correct data are being added, but when doing a readFromParcel() only the first element shows up and the second element is null.
Any help will be greatly appreciated!!!
Here's my list container class:
public class WfdDeviceList implements Parcelable {
private static final String TAG = "WfdDeviceList";
public List<WfdDevice> devList;
public WfdDeviceList() { devList = new ArrayList<WfdDevice>(); }
public WfdDeviceList(Parcel parcel) {
this();
readFromParcel(parcel);
}
public void clear() {
devList.clear();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
Log.d(TAG, "writeToParcel(): " + devList.size());
Log.d(TAG, "flags: " + flags);
for (WfdDevice device : devList) {
Log.d(TAG, "Writing: " + device.name);
}
parcel.writeTypedList(devList);
}
public void readFromParcel(Parcel parcel) {
Log.d(TAG, "readFromParcel()");
parcel.readTypedList(devList, WfdDevice.CREATOR);
Log.d(TAG, "Size: " + devList.size());
WfdDevice device = devList.get(0);
Log.d(TAG, "Device1: " + device.name);
}
public static final Parcelable.Creator<WfdDeviceList> CREATOR = new Parcelable
.Creator<WfdDeviceList>() {
public WfdDeviceList createFromParcel(Parcel in) {
return new WfdDeviceList(in);
}
public WfdDeviceList[] newArray(int size) {
return new WfdDeviceList[size];
}
};
}
public class WfdDevice implements Parcelable {
public String macAddress;
public String name;
public WfdDeviceType type;
public String ipAddr;
public String pin;
public boolean isConnected;
public WfdDevice() {}
public WfdDevice(Parcel parcel) {
readFromParcel(parcel);
}
@Override
public int describeContents() {
return 0;
}
WfdDeviceType getType(int type) {
switch(type) {
case 1:
return WfdDeviceType.source;
case 2:
return WfdDeviceType.primarySink;
case 3:
return WfdDeviceType.secondarySink;
case 4:
return WfdDeviceType.sourcePrimarySink;
default:
case 0:
return WfdDeviceType.unknown;
}
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeString(this.macAddress);
parcel.writeString(this.name);
parcel.writeInt(type.ordinal());
parcel.writeString(this.ipAddr);
parcel.writeString(this.pin);
parcel.writeInt(this.isConnected == true ? 1 : 0);
}
public void readFromParcel(Parcel parcel) {
this.macAddress = parcel.readString();
this.name = parcel.readString();
this.type = getType(parcel.readInt());
this.ipAddr = parcel.readString();
this.pin = parcel.readString();
this.isConnected = (parcel.readInt() == 1 ? true : false);
}
public static final Parcelable.Creator<WfdDevice> CREATOR = new Parcelable
.Creator<WfdDevice>() {
public WfdDevice createFromParcel(Parcel in) {
return new WfdDevice(in);
}
public WfdDevice[] newArray(int size) {
return new WfdDevice[size];
}
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我已经找到问题了。似乎是我的 WfdDevice 类中的枚举类型 WfdDeviceType 在某种程度上导致了该问题。我已经将该类型从枚举更改为整数,问题就消失了。
I think I've found the issue. It seems to be that having the enum type WfdDeviceType in my WfdDevice class was somehow causing the issue. I've since changed that type from an enum to an int and the problem has gone away.