无法将ArrayList类型的对象转换为我的模型

时间:2017-05-31 04:38:41

标签: java listview firebase arraylist firebase-realtime-database

在尝试使用firebase执行简单的聊天应用程序时,我遇到了下面记录的此错误。 我注意到的一件事是,之前的消息将加载,但我尝试刷到最后一条消息的实例,然后应用程序崩溃,同时给出下面的日志。 我将真诚地感谢帮助了解其原因。 感谢

  

com.google.firebase.database.DatabaseException:无法转换对象   类型为java.util.ArrayList,用于键入chat.ChatMessage                                                                                      在com.google.android.gms.internal.zzamy.zze(未知来源)                                                                                      在com.google.android.gms.internal.zzamy.zzb(未知来源)                                                                                      在com.google.android.gms.internal.zzamy.zza(未知来源)                                                                                      在com.google.firebase.database.DataSnapshot.getValue(未知来源)                                                                                      在   com.firebase.ui.database.FirebaseListAdapter.parseSnapshot(FirebaseListAdapter.java:120)                                                                                      在   com.firebase.ui.database.FirebaseListAdapter.getItem(FirebaseListAdapter.java:109)                                                                                      在   com.firebase.ui.database.FirebaseListAdapter.getView(FirebaseListAdapter.java:137)                                                                                      在android.widget.AbsListView.obtainView(AbsListView.java:2485)                                                                                      在android.widget.ListView.makeAndAddView(ListView.java:1945)                                                                                      在android.widget.ListView.fillDown(ListView.java:710)                                                                                      在android.widget.ListView.fillGap(ListView.java:674)                                                                                      在android.widget.AbsListView.trackMotionScroll(AbsListView.java:5534)                                                                                      在android.widget.AbsListView $ FlingRunnable.run(AbsListView.java:5030)                                                                                      在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:800)                                                                                      在android.view.Choreographer.doCallbacks(Choreographer.java:603)                                                                                      在android.view.Choreographer.doFrame(Choreographer.java:571)                                                                                      在   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:786)                                                                                      在android.os.Handler.handleCallback(Handler.java:815)                                                                                      在android.os.Handler.dispatchMessage(Handler.java:104)                                                                                      在android.os.Looper.loop(Looper.java:194)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:5682)                                                                                      at java.lang.reflect.Method.invoke(Native Method)                                                                                      在java.lang.reflect.Method.invoke(Method.java:372)                                                                                      在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:963)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)

这是我正在使用的ChatMessage模型

public class ChatMessage {

    private String messageText;
    private String messageUser;
    private long messageTime;

    public ChatMessage(String messageText, String messageUser) {
        this.messageText = messageText;
        this.messageUser = messageUser;
        messageTime = new Date().getTime();
    }

    public ChatMessage(){

    }

    public String getMessageText() {
        return messageText;
    }

    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    public String getMessageUser() {
        return messageUser;
    }

    public void setMessageUser(String messageUser) {
        this.messageUser = messageUser;
    }

    public long getMessageTime() {
        return messageTime;
    }

    public void setMessageTime(long messageTime) {
        this.messageTime = messageTime;
    }
}

我的displayChat消息是

private void displayChatMessages() throws DatabaseException {
        ListView listOfMessages = (ListView)findViewById(R.id.list_of_messages);
try{
        adapter = new FirebaseListAdapter<ChatMessage>(this, ChatMessage.class,
                R.layout.message_activity, FirebaseDatabase.getInstance().getReference()) {
            @Override
            protected void populateView(View v, ChatMessage model, int position) {
                // Get references to the views of message.xml
                TextView messageText = (TextView)v.findViewById(R.id.message_text);
                TextView messageUser = (TextView)v.findViewById(R.id.message_user);
                TextView messageTime = (TextView)v.findViewById(R.id.message_time);

                // Set their text
                messageText.setText(model.getMessageText());
                messageUser.setText(model.getMessageUser());
                messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",
                        model.getMessageTime()));
            }
        };

        listOfMessages.setAdapter(adapter);
    }catch (DatabaseException ex){
    ex.printStackTrace();
    }}

0 个答案:

没有答案