在反序列化时期望一个Map,但得到了一个类java.lang.Long

时间:2016-10-21 07:15:38

标签: java android firebase nullpointerexception firebase-realtime-database

我收到了这个错误 “在反序列化时预期映射,但在使用firebase数据库时得到了一个类java.lang.Long”。 我在stackoverflow上查找了一些问题同样的问题,但没有找到相关的解决方案。

这是我的POJO类,用于存储来自firebase的数据。

public class News {
public String topic;
public String detail;
public String user;
public Map<String, String> timestamp = null;

public News(){

}

public News(String topic, String detail, String user, HashMap<String, String> timeStamp){
    this.topic = topic;
    this.detail = detail;
    this.user = user;
    this.timestamp = timeStamp;
    //this.timestamp.put("timestamp", timeStamp);
    //this.timestamp.put("timestamp", ServerValue.TIMESTAMP);
}

public String getTopic(){
    return topic;
}

public String getDetail(){
    return detail;
}

@Exclude
public Map<String, Object> toMap() {
    HashMap<String, Object> result = new HashMap<>();

    result.put("topic", topic);
    result.put("detail", detail);
    result.put("user", user);
    result.put("timestamp", timestamp);

    return  result;
}
}

这是我从firebase获取数据的主要类。

public class News_M extends AppCompatActivity {

FirebaseListAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news);

    ListView messagesView = (ListView) findViewById(R.id.listview);

    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("abc").child("news");
    //News msg = new News("Holiday", "28 October", "fayeed", ServerValue.TIMESTAMP);
    //ref.push().setValue(msg);

    mAdapter = new FirebaseListAdapter<News>(this, News.class, android.R.layout.two_line_list_item, ref) {
        @Override
        protected void populateView(View view, News newsMessage, int position) {
            ((TextView)view.findViewById(android.R.id.text1)).setText(newsMessage.getTopic());
                 ((TextView)view.findViewById(android.R.id.text2)).setText(newsMessage.getDetail(      ));
        }
    };

    messagesView.setAdapter(mAdapter);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mAdapter.cleanup();
}
}

0 个答案:

没有答案