I am having trouble with implementing a Parcelable class in android.
The issue i am having is that i am receiving is that BinarySearchTree cannot be cast to a TreeNode.
When i print out what the type is when i am recieving it says tree node so im not entirely sure what the issue is.
This is the error
01-31 18:13:50.986: E/AndroidRuntime(1059): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.discoverycalendar/com.discoverycalendar.DiscoveryMainMenu}:
java.lang.ClassCastException: com.DiscoveryUtils.BinarySearchTree cannot be cast to com.DiscoveryObjects.TreeNode
private TreeNode root = null;
@Override
public int describeContents() {
return 0;
}
public BinarySearchTree(Parcel in){
this();
root = (TreeNode) in.readValue(TreeNode.class.getClassLoader());
in.readStringList(treeCont);
in.readStringList(treeAsString);
}
public static final Creator<BinarySearchTree> CREATOR = new Creator<BinarySearchTree>() {
public BinarySearchTree createFromParcel(Parcel source) {
return new BinarySearchTree(source);
}
public BinarySearchTree[] newArray(int size) {
return new BinarySearchTree[size];
}
};
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(root);
dest.writeStringList(treeCont);
dest.writeStringList(treeAsString);
}
The error i am receiving is when i try casting the return value into a TreeNode. I dont know why its thinking its a different object.
The simple reason is TreeNode is not Serializable or Parcelable. Hence you need to make a custom class that extends TreeNode and impelemtn one of them in order to flatten the data.
Here is a document of support types. TreeNode isn’t there.