无法退回键入,添加其他值时聊天示例错误,

时间:2016-04-16 04:22:29

标签: java android firebase firebase-realtime-database

我使用Firebase开发了聊天应用。该应用程序运行正常,但在我添加datetime值后,我面临此错误:

04-16 09:23:11.156 5790-5790/learning.firebase.app.learningfirebase E/AndroidRuntime: FATAL EXCEPTION: main
    com.firebase.client.FirebaseException: Failed to bounce to type
        at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
        at learning.firebase.app.learningfirebase.FirebaseListAdapter$1.onChildAdded(FirebaseListAdapter.java:63)
        at com.firebase.client.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:48)
        at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
        at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5365)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class learning.firebase.app.learningfirebase.Chat]: can not instantiate from JSON object (need to add/enable type information?)
     at [Source: java.io.StringReader@41b8e358; line: 1, column: 2]
        at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:984)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:276)
        at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
        at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
        at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
        at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:181)

我的代码如下所示

Chat.java

public class Chat {

private String message;
private String author;
private String datetime;

// Required default constructor for Firebase object mapping
@SuppressWarnings("unused")

Chat(String message, String author,String datetime) {
    this.message = message;
    this.author = author;
    this.datetime=datetime;
}

public String getMessage() {
    return message;
}

public String getAuthor() {
    return author;
}

public String getDatetime() {
    return datetime;
}
}

 @Override
protected void populateView(View view, Chat chat) {
    // Map a Chat object to an entry in our listview
    String author = chat.getAuthor();
    TextView authorText = (TextView) view.findViewById(R.id.author);
    TextView datetime=(TextView)view.findViewById(R.id.datetime);
    authorText.setText(author + ": ");
    // If the message was sent by this user, color it differently
    if (author != null && author.equals(mUsername)) {
        authorText.setTextColor(Color.RED);
        datetime.setTextColor(Color.GREEN);
    } else {
        authorText.setTextColor(Color.BLUE);
        datetime.setTextColor(Color.GREEN);
    }
    ((TextView) view.findViewById(R.id.message)).setText(chat.getMessage());
    ((TextView) view.findViewById(R.id.datetime)).setText(chat.getDatetime());

}

和发送消息代码

private void sendMessage() {
    EditText inputText = (EditText) findViewById(R.id.messageInput);
    String input = inputText.getText().toString();
    if (!input.equals("")) {
        // Create our 'model', a Chat object
        Chat chat = new Chat(input, mUsername,"Today : ");
        // Create a new, auto-generated child of that chat location, and save our chat data there
        mFirebaseRef.push().setValue(chat);
        inputText.setText("");

        Bundle b=new Bundle();
        b.putString("mUsername",mUsername);
        b.putString("mMessage",input);

        Intent intent = new Intent();
        intent.putExtras(b);
        intent.setAction("learning.firebase.app.learningfirebase.CUSTUM_INTENT");
        sendBroadcast(intent);

    }
}

JSON规则

   {
  "chat": {
      "author:1005213"
      "datetime": "Today :",
      "message": "hello"

  }
}

0 个答案:

没有答案