2016-08-15 31 views
1

此問題/錯誤的根本原因是什麼?如何解決它?我的數據模型結構不正確嗎?或者我做錯了查詢?DatabaseException:反序列化時期望映射,但得到類java.lang.Long

//添加新的評論

String comment = medit.getText().toString(); 
Comment newComment = new Comment(user,comment,postKey); 
db.child("Post-Comments").child(postKey).push().setValue(newComment); 

//檢索所有評論在後 //使用FirebaseRecyclerView和片段

DatabaseReference mRef = db.child("Post-Comments").child(postKey).getRef(); 

adapter = new FirebaseRecyclerAdapter<Comment, myViewHolder> (Comment.class, 
R.layout.comment_list_layout,myViewHolder.class, mRef) 
    { 
     @Override 
     protected void populateViewHolder(myViewHolder viewHolder, 
      Comment model, int position) { 


      viewHolder.bindToComments(model); 
     } 
    }; 

    rlm2 = new LinearLayoutManager(getActivity()); 
    rv2.setLayoutManager(rlm2); 
    rv2.setAdapter(adapter); 

//評論類

public class Comment { 

public String comment; 
public String reactUserId; 
public Map<String,String> created_on; 
public String postKey; 



public Comment() { 
} 

public Comment(String reactUserId, String comment, String postKey) { 

    this.reactUserId = reactUserId; 
    this.postKey = postKey; 
    this.comment = comment; 
    this.created_on = ServerValue.TIMESTAMP; 

} 


public Map<String,Object> toMap(){ 
    Map<String,Object> values = new HashMap<>(); 

    values.put("userId",reactUserId); 
    values.put("postKey",postKey); 
    values.put("comment",comment); 
    values.put("created_on",created_on); 

    return values; 

} 
}// more getter and setters 

// Stacktrace

10745-10745/com.dhiraj.firebaseapp E/AndroidRuntime: FATAL EXCEPTION: main 
com.google.firebase.database.DatabaseException: 
Expected a Map while deserializing, but got a class java.lang.Long 

// JSON從火力地堡DB

{ 
"Post-Comments" : { 
"-KPE1qy6FgcwOnb1YXRw" : 
    "-KPE1zMFviLJvEKxexB7" : { 
    "AUTHOR" : "Dheeraj", 
    "comment" : "the first time I am not sure if you have any questions please ", 
    "created_on" : 1471278741281, 
    "postKey" : "-KPE1qy6FgcwOnb1YXRw", 
    "reactUserId" : "Dheeraj" 
    }, 
    "-KPE221Xk5t9aMJfYoKD" : { 
    "AUTHOR" : "Dheeraj", 
    "comment" : "the first time I am not sure if you have any questions please ", 
    "created_on" : 1471278756332, 
    "postKey" : "-KPE1qy6FgcwOnb1YXRw", 
    "reactUserId" : "Dheeraj" 
    } 
} 
    }, 
"Posts" : { 
    "-KPE1qy6FgcwOnb1YXRw" : { 
    "created on" : 1471278706910, 
    "desc" : "the only one who has been sent to you and your family and friends are invited to view the full image to open ", 
    "title" : "the following link ", 
    "userId" : "Dhiraj" 
}, 
"-KPE1vlYc5vF7AyL2OHL" : { 
    "created on" : 1471278726738, 
    "desc" : "hi I was thinking that it will take place at all and all the best way is to have ", 
    "title" : "good afternoon ", 
    "userId" : "Dhiraj" 
} 
}, 

} 
+0

你能不能編輯你的問題來顯示Comment類的樣子? – Bill

+0

這是拋出此錯誤的確切線?你可以發佈你的錯誤堆棧跟蹤嗎? –

+0

除了前面兩個問題之外,還可以發佈'Post-Comments/'中的代表片段JSON(作爲實際的JSON文本,請勿截圖)。 –

回答

5
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference("Post-Comments") 

Query queryRef = mFirebaseDatabaseReference.orderByKey(); 
      queryRef.addValueEventListener(new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot dataSnapshot) { 
         if (dataSnapshot != null && dataSnapshot.getValue() != null) { 
         Log.e("Count ", "" + dataSnapshot.getChildrenCount()); 
         Map<String, Object> objectMap = (HashMap<String, Object>) 
           dataSnapshot.getValue(); 
         ArrayList<ItemsReceived> itemsReceivedList = new ArrayList<>(); 

         for (Object obj : objectMap.values()) { 
          if (obj instanceof Map) { 
           Map<String, Object> mapObj = (Map<String, Object>) obj; 
           ItemsReceived itemsReceived = new ItemsReceived(); 
           itemsReceived.setAccount((String) mapObj.get("AUTHOR")); 
           itemsReceived.setAdded((long) mapObj.get("comment")); 

           itemsReceivedList.add(itemsReceived); 

           Log.e(TAG, "Data is" + itemsReceived.getAdded()); 


          } 
         } 

         itemsRecievedListAdapter = new ItemsRecievedListAdapter(MainActivity.this, itemsReceivedList); 
         mRecyclerView.setAdapter(itemsRecievedListAdapter); 
    // 
        } 
       } 

       @Override 
       public void onCancelled(DatabaseError databaseError) { 

       } 
      });` 

替換我的變量在必要時我只是想提出一個觀點,並不能徹底解決......我希望它能幫助

+0

非常感謝! –

+0

隨時恭維Goodlife – Goodlife

相關問題