2016-07-18 19 views
0

gegossen werden, wenn ich versuche, durch „loop für“ iteriert wird, was zu einer schwerwiegenden Ausnahme:Android: Loop für sich java.lang.ClassCastException bekommen: com.google.gson.internal.LinkedTreeMap nicht

   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) 

ich habe diese Daten:

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


chatUser2 = {[email protected]} size = 2 
0 = {[email protected]} size = 14 
    0 = {[email protected]} "ConnectionId" -> "2500c795-3bad-4543-9905-2693807cc8ea" 
     key = "ConnectionId" 
     value = "2500c795-3bad-4543-9905-2693807cc8ea" 
    1 = {[email protected]} "UserGroup" -> "G" 
     key = "UserGroup" 
     value = "G" 
    2 = {[email protected]} "FgBusy" -> "S" 
    3 = {[email protected]} "UserType" -> "O" 
    4 = {[email protected]} "OperatorConnectionId" -> "null" 
    5 = {[email protected]} "OperatorID" -> "2.0" 
    6 = {[email protected]} "OperatorName" -> "null" 
    7 = {[email protected]} "OnlineUsers" -> "0.0" 
    8 = {[email protected]} "UserID" -> "2.0" 
    9 = {[email protected]} "UserName" -> "Douglas" 
    10 = {[email protected]} "CurrentDate" -> "2016-07-18T10:41:31.6714956-03:00" 
    11 = {[email protected]} "Latitude" -> "47.677928" 
    12 = {[email protected]} "Longitude" -> "-122.130293" 
    13 = {[email protected]} "VehicleId" -> "17.0" 
1 = {[email protected]} size = 14 
    0 = {[email protected]} "ConnectionId" -> "9d5fa748-0870-419c-82b3-0a2c2654839e" 
    1 = {[email protected]} "UserGroup" -> "G" 
    2 = {[email protected]} "FgBusy" -> "S" 
    3 = {[email protected]} "UserType" -> "O" 
    4 = {[email protected]} "OperatorConnectionId" -> "null" 
    5 = {[email protected]} "OperatorID" -> "1.0" 
    6 = {[email protected]} "OperatorName" -> "null" 
    7 = {[email protected]} "OnlineUsers" -> "0.0" 
    8 = {[email protected]} "UserID" -> "1.0" 
    9 = {[email protected]} "UserName" -> "Wilian" 
10 = {[email protected]} "CurrentDate" -> "2016-07-18T10:41:37.1728196-03:00" 
11 = {[email protected]} "Latitude" -> "47.678694" 
12 = {[email protected]} "Longitude" -> "-122.130529" 
13 = {[email protected]} "VehicleId" -> "16.0" 

ich habe versucht, auf diese Weise iterieren:

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

ich andere Wege versucht haben, versucht Gson, itera Tor usw. Aber ich konnte nicht lösen. Jeder kann mir helfen ??

Vielen Dank im Voraus.

+0

wie funktioniert chatUser2 initialisiert und bevölkert werden? – Bill

+2

Ihr JSON-Parser (GSON, glaube ich?) Analysiert dieses Objekt nicht in einer Klasse - so fällt es zurück, es in einen Baum zu verwandeln. Wenn Sie dann versuchen, es als ChatUser zu speichern, löst es eine Ausnahme aus. Veröffentlichen Sie Ihren JSON-Parsing-Code. –

+0

Ich habe die ArrayList vom Abrufen einer Absichtsnachricht ausgefüllt: ArrayList chatUser2 = intent.getParcelableArrayListExtra ("ChatUser"). Ich habe nur Probleme, wenn ich versuche, Daten per Schleife für zu bekommen. –

Antwort

0

I gelöst Parcel Implementierung und Änderung der 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]; 
     } 
    }; 
}