我正在尝试通过意图将一个类传递给另一个活动,并且在创建活动之前出现了一些错误。
以下是主要活动中的电话:
Intent intent = new Intent(this, NavigateChartActivity.class);
TreeNode selectedOption = chart.getChild(position);
Bundle b = new Bundle();
b.putParcelable("Selected", selectedOption);
intent.putExtras(b);
startActivity(intent);
这是另一个类,也许我在Parcelable部分实现了错误:
public class TreeNode implements Parcelable {
String data = "";
TreeNode parent = null;
List<TreeNode> children;
String question = "";
public TreeNode(String data) {
this.data = data;
this.children = new LinkedList<TreeNode>();
this.question = "";
}
public TreeNode(String data, String q) {
this.data = data;
this.children = new LinkedList<TreeNode>();
this.question = q;
}
public TreeNode() {
}
public TreeNode(Parcel parcel) {
data = parcel.readString();
parent = parcel.readTypedObject(TreeNode.CREATOR);
parcel.readTypedList(children, TreeNode.CREATOR);
question = parcel.readString();
}
public TreeNode addChild(TreeNode child) {
child.parent = this;
this.children.add(child);
return child;
}
public void addMultipleChilds(TreeNode[] childs){
for(int i = 0; i < childs.length;i++){
addChild(childs[i]);
}
}
public TreeNode getChild(int i){
return this.children.get(i);
}
public String[] getListOfData(){
String[] options = new String[this.children.size()];
for(int i = 0;i<this.children.size();i++){
options[i] = this.getChild(i).data.toString();
}
return options;
}
// Parcelable implementation
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(data);
parcel.writeTypedObject(parent, i);
parcel.writeTypedList(children);
parcel.writeString(question);
}
public static Creator<TreeNode> CREATOR = new Creator<TreeNode>() {
public TreeNode createFromParcel(Parcel parcel) {
return new TreeNode(parcel);
}
public TreeNode[] newArray(int size) {
return new TreeNode[size];
}
};
FinalNode类也是Parcelable,具有相同的实现,我将在必要时发布它
感谢您的帮助