在我的应用程序中,我有“MyService”类,它从电话中的某些数据进行备份,这是在run()中的线程中。也在另一个班级我有这个:
public static final Parcelable.Creator<ParentActivityData> CREATOR = new Creator<ParentActivityData>() {
@Override
public ParentActivityData createFromParcel(Parcel parcel) {
return null;
}
我不在任何地方使用它,当我按下备份按钮时,我收到此错误:
ERROR/AndroidRuntime(1389): FATAL EXCEPTION: Thread-99
java.lang.NullPointerException
at com.---.---.MyService$1.run(MyService.java:51)
我该如何解决?
编辑:
package com.athena.paadbox.entity;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.HashMap;
import java.util.List;
public class ParentActivityData implements Parcelable {
public List<SelectableEntity> listDataHeader;
public HashMap<String, List<SelectableEntity>> listDataChild;
public static final Parcelable.Creator<ParentActivityData> CREATOR = new Creator<ParentActivityData>() {
@Override
public ParentActivityData createFromParcel(Parcel parcel) {
ParentActivityData pad = new ParentActivityData();
return pad;
}
@Override
public ParentActivityData[] newArray(int i) {
return new ParentActivityData[0];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
}
}
编辑:
public class SelectableEntity implements Parcelable {
public SelectableEntity parent = null;
public String title = null;
public boolean state = false;
public SelectableEntity(String title) {
this.title = title;
}
public SelectableEntity(SelectableEntity parent, String title) {
this.parent = parent;
this.title = title;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
}
}
答案 0 :(得分:1)
试试这个......
public class ParentActivityData implements Parcelable {
public List<SelectableEntity> listDataHeader;
public HashMap<String, List<SelectableEntity>> listDataChild;
public ParentActivityData() {
}
private ParentActivityData(Parcel parcel) {
listDataHeader = (List<SelectableEntity>) parcel.readValue(getClass().getClassLoader());
listDataChild = (HashMap<String, List<SelectableEntity>>) parcel.readValue(getClass().getClassLoader());
}
public static final Parcelable.Creator<ParentActivityData> CREATOR = new Parcelable.Creator<ParentActivityData>() {
@Override
public ParentActivityData createFromParcel(Parcel parcel) {
return new ParentActivityData(parcel);
}
@Override
public ParentActivityData[] newArray(int size) {
return new ParentActivityData[size];
}
};
@Override
public int describeContents() {
return 0xFEED;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeValue(listDataHeader);
parcel.writeValue(listDataChild);
}
}
public class SelectableEntity implements Parcelable {
public SelectableEntity parent = null;
public String title = null;
public boolean state = false;
// Don't use these constructors.
/*public SelectableEntity(String title) {
this.title = title;
}
public SelectableEntity(SelectableEntity parent, String title) {
this.parent = parent;
this.title = title;
}*/
public SelectableEntity() {
}
// use setters
public void setParent(SelectableEntity parent) {
this.parent = parent;
}
public void setTitle(String title) {
this.title = title;
}
public void setState(boolean state) {
this.state = state;
}
private SelectableEntity(Parcel parcel) {
parent = parcel.readParcelable(getClass().getClassLoader());
title = parcel.readString();
state = (Boolean) parcel.readValue(getClass().getClassLoader());
}
@Override
public int describeContents() {
return 0xFEED;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeParcelable(parent, flags);
parcel.writeString(title);
parcel.writeValue((Boolean)state);
}
public static final Parcelable.Creator<SelectableEntity> CREATOR = new Parcelable.Creator<MainActivity.SelectableEntity>() {
@Override
public SelectableEntity createFromParcel(Parcel source) {
return new SelectableEntity(source);
}
@Override
public SelectableEntity[] newArray(int size) {
return new SelectableEntity[size];
}
};
}
为此,要使用,您的SelectableEntity类还必须实现Parcelable。