Start a new topic

How to Serialize a GenericJson extended class ??

Hi,


I have a class named "Parent" extends GenericJson class. Now i want to pass object of this class from one activity to another. One method is make it serializable but it throws below exception :


Exception : java.lang.ClassCastException: java.util.HashMap cannot be cast to com.mypackage.Parent



Please help how can i resolve this problem.


1 person has this question

Sukhpal, 


I can't help you without seeing the code that is making you have this issue.


Thanks,

Have a look to https://support.kinvey.com/support/discussions/topics/5000041631


2 people like this

Fabreax is correct on this one, Sukhpal, if you take a look at the comment directly above yours in that thread, it has the solution to this question.


Thanks,


1 person likes this

Thnx Fabreax and Damien 


I already seen this but still have some problem it shows error


public Child(HashMap map){

 

  for(String key : map.keySet()){

   this.put(key, map.get(key));

  }

 }


i put this constructor in class but is show error : Type mismatch cannot convert from element type object to string.



This is my code..


  

package com.info.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import com.google.api.client.json.GenericJson;
import com.google.api.client.util.Key;
import com.google.gson.annotations.SerializedName;
import com.kinvey.java.model.KinveyMetaData;

public class Parent extends GenericJson implements Serializable {
	
	
	@Key("_id")
	private String id;
	
	@Key("deviceId")
	private String deviceId;
	
	@Key("ParentId")
	private String parentId;
	
	@Key("NumberOfChild")
	private String numberOfChild;
	
	@Key("ListofActiveChild")
	private ArrayList<Child> listofActiveChild;
	
	@Key("_kmd")
	private KinveyMetaData meta; // Kinvey metadata
	
	@Key("_acl")
	private KinveyMetaData.AccessControlList acl; // Kinvey Access Control
	
	public Parent(){
		
		this.deviceId = new String();
		this.parentId = new String();
		this.numberOfChild = new String();
		this.listofActiveChild = new ArrayList<Child>();
		
		meta = new KinveyMetaData();
        acl = new KinveyMetaData.AccessControlList();
	}

	public Parent(HashMap map){
		 
		  for(String key : map.keySet()){
		   this.put(key, map.get(key));
		  }
		 }

	public String getActionId(){
		return id;
	}
	
	public String getDeviceId() {
		return deviceId;
	}
	
	public String getParentId() {
		return parentId;
	}

	public String getNumberOfChild() {
		return numberOfChild;
	}

	public ArrayList<Child> getListOfActiveChild() {
		return listofActiveChild;
	}

	public void setActionId(String id){
		this.id=id;
	}
	
	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}
	
	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public void setNumberOfChild(String numberOfChild) {
		this.numberOfChild = numberOfChild;
	}

	public void setListOfActiveChild(ArrayList<Child> listOfActiveChild) {
		this.listofActiveChild = listOfActiveChild;
	}	
	
	public KinveyMetaData getMeta() {
        return meta;
    }

    public void setMeta(KinveyMetaData meta) {
        this.meta = meta;
    }

    public KinveyMetaData.AccessControlList getAcl() {
        return acl;
    }

    public void setAcl(KinveyMetaData.AccessControlList acl) {
        this.acl = acl;
    }
    
}

  

this show error at line - > for(String key: map.keySet()) // Type mismatch cannot convert from element type object to string.


please tell me where i put something wrong .. 


thnx in advance 


1 person likes this

You should try to specify types :

public Parent(HashMap<String, Object> map){
   for(String key : map.keySet()){
    this.put(key, map.get(key));
  }
}

 

Login or Signup to post a comment