Clever Almeida Clever Almeida - 1 month ago 18
Android Question

Android: Loop for is getting java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast

When I'm trying to iterate by "loop for" is resulting a Fatal Exception:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to br.com.gerticonsultoria.easyrentrider.model.ChatUser
at br.com.gerticonsultoria.easyrentrider.MapActivity$1.onReceive(MapActivity.java:272)
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)


I have these datas:

ArrayList<ChatUser> chatUser2 = intent.getParcelableArrayListExtra("chatUser");


chatUser2 = {ArrayList@6170} size = 2
0 = {LinkedTreeMap@6529} size = 14
0 = {LinkedTreeMap$Node@6533} "ConnectionId" -> "2500c795-3bad-4543-9905-2693807cc8ea"
key = "ConnectionId"
value = "2500c795-3bad-4543-9905-2693807cc8ea"
1 = {LinkedTreeMap$Node@6534} "UserGroup" -> "G"
key = "UserGroup"
value = "G"
2 = {LinkedTreeMap$Node@6535} "FgBusy" -> "S"
3 = {LinkedTreeMap$Node@6536} "UserType" -> "O"
4 = {LinkedTreeMap$Node@6537} "OperatorConnectionId" -> "null"
5 = {LinkedTreeMap$Node@6538} "OperatorID" -> "2.0"
6 = {LinkedTreeMap$Node@6539} "OperatorName" -> "null"
7 = {LinkedTreeMap$Node@6540} "OnlineUsers" -> "0.0"
8 = {LinkedTreeMap$Node@6541} "UserID" -> "2.0"
9 = {LinkedTreeMap$Node@6542} "UserName" -> "Douglas"
10 = {LinkedTreeMap$Node@6543} "CurrentDate" -> "2016-07-18T10:41:31.6714956-03:00"
11 = {LinkedTreeMap$Node@6544} "Latitude" -> "47.677928"
12 = {LinkedTreeMap$Node@6545} "Longitude" -> "-122.130293"
13 = {LinkedTreeMap$Node@6546} "VehicleId" -> "17.0"
1 = {LinkedTreeMap@6530} size = 14
0 = {LinkedTreeMap$Node@6606} "ConnectionId" -> "9d5fa748-0870-419c-82b3-0a2c2654839e"
1 = {LinkedTreeMap$Node@6607} "UserGroup" -> "G"
2 = {LinkedTreeMap$Node@6608} "FgBusy" -> "S"
3 = {LinkedTreeMap$Node@6609} "UserType" -> "O"
4 = {LinkedTreeMap$Node@6610} "OperatorConnectionId" -> "null"
5 = {LinkedTreeMap$Node@6611} "OperatorID" -> "1.0"
6 = {LinkedTreeMap$Node@6612} "OperatorName" -> "null"
7 = {LinkedTreeMap$Node@6613} "OnlineUsers" -> "0.0"
8 = {LinkedTreeMap$Node@6614} "UserID" -> "1.0"
9 = {LinkedTreeMap$Node@6615} "UserName" -> "Wilian"
10 = {LinkedTreeMap$Node@6616} "CurrentDate" -> "2016-07-18T10:41:37.1728196-03:00"
11 = {LinkedTreeMap$Node@6617} "Latitude" -> "47.678694"
12 = {LinkedTreeMap$Node@6618} "Longitude" -> "-122.130529"
13 = {LinkedTreeMap$Node@6619} "VehicleId" -> "16.0"


I tried to iterate this way:

for (int i = 0; i < chatUser2.size(); i++) {
Log.i("script", "Coords : " + chatUser2.get(i).Latitude + " " + chatUser2.get(i).Longitude);
}


I've tried other ways, tried gson, iterator, etc. but I couldn't solve. Anyone can help me ??

Thanks in advance.

Answer

I solved implementing Parcelable and changing the iteration:

    for (int i = 0; i < chatUserConnect.size(); i++) {
                listMapItem.add(new MapItem(chatUserConnect.get(i).getLatitude(), chatUserConnect.get(i).getLongitude(),"" ,
                        chatUserConnect.get(i).getUserID(), chatUserConnect.get(i).getVehicleId()));
     }





import android.os.Parcel;
import android.os.Parcelable;

import com.google.gson.annotations.SerializedName;


public  class ChatUserConnect implements Parcelable
{
    @SerializedName("UserType")
    public String UserType;
    @SerializedName("UserID")
    public long UserID;
    @SerializedName("Latitude")
    public double Latitude;
    @SerializedName("Longitude")
    public double Longitude;
    @SerializedName("VehicleId")
    public long VehicleId;

    public ChatUserConnect( String UserType, long UserID, double Latitude, double Longitude, long VehicleId ) {
        this.UserType = UserType;
        this.UserID = UserID;
        this.Latitude = Latitude;
        this.Longitude = Longitude;
        this.VehicleId = VehicleId;
    }

    public long getVehicleId() {
        return VehicleId;
    }

    public void setVehicleId(long VehicleId) {
        this.VehicleId = VehicleId;
    }

    public String getUserType() {
        return UserType;
    }

    public void setUserID(String UserType) {
        this.UserType = UserType;
    }

    public long getUserID() {
        return UserID;
    }

    public void setUserID(long UserID) {
        this.UserID = UserID;
    }

    public double getLongitude() {
        return Longitude;
    }
    public void setLongitude(double Longitude) {
        this.Longitude = Longitude;
    }

    public double getLatitude() {
        return Latitude;
    }
    public void setLatitude(double Latitude) {
        this.Latitude = Latitude;
    }



    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(UserType);
        dest.writeLong(UserID);
        dest.writeDouble(Latitude);
        dest.writeDouble(Longitude);
        dest.writeLong(VehicleId);
    }
    protected ChatUserConnect(Parcel in) {
        UserType = in.readString();
        UserID = in.readLong();
        Latitude = in.readDouble();
        Longitude = in.readDouble();
        VehicleId = in.readLong();
    }

    public static final Creator<ChatUserConnect> CREATOR  = new Creator<ChatUserConnect>() {
        @Override
        public ChatUserConnect createFromParcel(Parcel in) {
            return new ChatUserConnect(in);
        }

        @Override
        public ChatUserConnect[] newArray(int size) {
            return new ChatUserConnect[size];
        }
    };
}