java - Firebase,类映射器 : no setter/field found for 'UserTwo' on class 'myclass'

标签 java android json firebase firebase-realtime-database

因此,firebase 不断给我空指针异常,而且它在“ChatModel”类上找不到任何 setter /字段,这是我的 chatModel 类:

package edusolution.matrimony;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by el on 8/26/16.
 */
public class ChatModel
{
    public Map<String,Object> message = new HashMap<>();
    public String foruser;
    public String foruserAnother;

    public  ChatModel()
    {

    }

    public  ChatModel(String foruser,String foruserAnother)
    {
        this.foruser = foruser;
        this.foruserAnother = foruserAnother;
    }

    public String getForuser()
    {
        return foruser;
    }

    public void setForuser(String foruser)
    {
        this.foruser = foruser;
    }

    public String getForuserAnother()
    {
        return foruserAnother;
    }

    public void setForuserAnother(String foruserAnother)
    {
        this.foruserAnother = foruserAnother;
    }

    public Map<String, Object> getMessage()
    {
        return message;
    }

    public void setMessage(Map<String,Object> message)
    {
        this.message = message;
    }

    public Map<String,Object> toMap()
    {
        HashMap<String,Object> result = new HashMap<>();
        message.put("somebody","iusedtoknow");
        result.put("UserOne",foruser);
        result.put("UserTwo",foruserAnother);
        result.put("Messages",message);
        return result;
    }
}

p.s 我尝试将字段设为私有(private),但没有成功,以下是我在数据库中创建聊天模型的方法:

private void createChatModel (String Name,String currentUserName)
    {
        DatabaseReference database = FirebaseDatabase.getInstance().getReference();
        String Key = FirebaseDatabase.getInstance().getReference().child("chatRooms").push().getKey();

        ChatModel chat = new ChatModel(Name,currentUserName);
        Map<String,Object> postValues = chat.toMap();
        Map<String,Object> childUpdates = new HashMap<>();
        childUpdates.put("/chatRooms/"+Key,postValues);
        database.updateChildren(childUpdates);
    }

最后我如何获取这些数据:

public void setupChat(final String username)
    {
        //check if chat room is already exists or not
        databaseReference.child("chatRooms").addValueEventListener(new ValueEventListener()
        {
            public void onDataChange(DataSnapshot dataSnapshot)
            {
                for(DataSnapshot s : dataSnapshot.getChildren())
                {
                    ChatModel chat = s.getValue(ChatModel.class);
                    if(chat != null )
                    {
                        Log.d("Key and hash : ",s.getKey()+" "+chat.getMessage().get(0));
                        if (chat.getForuser().equals(auth.getCurrentUser().getUid()) && chat.getForuserAnother().equals(username))
                        {
                            Log.d("Chat is ", s.getKey());
                            RoomName = new String(s.getKey());
                            ChatEnabled = true;
                        }
                        else if (chat.getForuser().equals(username) && chat.getForuserAnother().equals(auth.getCurrentUser().getUid()))
                        {
                            Log.d("chat is also ", s.getKey());
                            RoomName = new String(s.getKey());
                            ChatEnabled = true;
                        }
                        else
                        {
                            Log.d("No chat : ", "creating chat");
                        }
                    }
                }
            }

            public void onCancelled(DatabaseError databaseError)
            {
                Toast.makeText(getApplicationContext(),"Unable to retrieve data",Toast.LENGTH_LONG).show();
            }
        });
    }

示例数据,保存在firebaseddatabase中,图片:Saved data image example

最后我每次都会遇到错误:

08-28 18:28:51.005 31762-31762/edusolution.matrimony W/ClassMapper: No setter/field for UserTwo found on class edusolution.matrimony.ChatModel
08-28 18:28:51.005 31762-31762/edusolution.matrimony W/ClassMapper: No setter/field for Messages found on class edusolution.matrimony.ChatModel
08-28 18:28:51.005 31762-31762/edusolution.matrimony W/ClassMapper: No setter/field for UserOne found on class edusolution.matrimony.ChatModel
08-28 18:28:51.005 31762-31762/edusolution.matrimony D/Key and hash :: -KQGYLHb2IQ34AHkzx5G null
08-28 18:28:51.005 31762-31762/edusolution.matrimony D/AndroidRuntime: Shutting down VM
08-28 18:28:51.005 31762-31762/edusolution.matrimony W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41919700)
08-28 18:28:51.012 31762-31762/edusolution.matrimony E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerExceptio
    at edusolution.matrimony.Chat$1.onDataChange(Chat.java:70)
    at com.google.android.gms.internal.zzaie.zza(Unknown Source)
    at com.google.android.gms.internal.zzaje.zzcta(Unknown Source)
    at com.google.android.gms.internal.zzajh$1.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

最佳答案

POJO类与数据库的结构不同。

将您的 ChatModel 类字段更改为

public class ChatModel {
    public Map<String,Object> Messages = new HashMap<>();
    public String UserOne;
    public String UserTwo;

    // constructor
    // setter and getter
}

关于java - Firebase,类映射器 : no setter/field found for 'UserTwo' on class 'myclass' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39192390/

相关文章:

java - 使用 iText 5 生成 pdf 文件时出现问题

java - 如何找到存储在链表中的值而不是存储在 HashMap 中的值

java - 将 galera mariadb 与 jdbc 结合使用

android - 标题文本与 CollapsingToolbarLayout 中的标题后退按钮重叠

android - 背景图像没有出现在某些使用手机间隙的 Android 设备上?

java - 来自 Json 的 Android 登录屏幕

java - 迭代 Hashmap 会导致崩溃

java - 如何使 JsonGenerator 漂亮地打印 Date 和 DateTime 值?

json - 如何获取 JSON 中某个键的值?

java - 在 fragment 之间切换时保存并重新加载 edittext 值