java - DatabaseException : Failed to convert value of type java. util.HashMap 到字符串 - Firebase

标签 java android firebase firebase-realtime-database

在我的项目中,我正在使用 Firebase 身份验证实现登录系统。由于我使用电子邮件登录,因此我无法在身份验证中存储任何用户的名称 - 相反,我将它们作为 userId 的子项存储在实时数据库中。所以我的数据库结构如下所示:

fireapp 
   +users
     +userID
       Name: <name>

每个用户都会根据其 ID 拥有一条记录。

当我的用户登录时,他们将被带到带有抽屉导航的主屏幕。在此抽屉的标题中,我想显示用户的姓名和电子邮件。到目前为止,我已经使用 FirebaseAuth 设置了电子邮件,但事实证明设置名称更加困难。

这是我当前使用的代码:

String userId = mAuth.getUid();
TextView userEmail = (TextView) findViewById(R.id.lblHeaderName);

FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference ref = db.getReference("users/" + userId);
ref.addValueEventListener(new ValueEventListener()
{
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot)
    {
        //Retrieve the user's name and set the textview in the header accordingly
        String name = dataSnapshot.getValue(String.class);
        userName.setText(name);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError)
    {
        //If the task is not successful, print an error message
        Toast.makeText(HomeScreen.this, "Error retrieving data from database: " + databaseError.getCode(), Toast.LENGTH_LONG).show();

    }
});

在这里,我首先获取当前用户的 ID 并将其用作数据库路径。接下来,我尝试获取用户的名称并将其分配给一个字符串(因为这是我为每个用户存储的唯一属性)。最后将其设置为用户名的文本。当我运行代码时,应用程序崩溃并出现以下错误:

2019-05-01 12:54:33.882 6447-6447/com.mobileappscw3.nrcforums E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mobileappscw3.nrcforums, PID: 6447
    com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(com.google.firebase:firebase-database@@16.1.0:413)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.1.0:199)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.1.0:79)
        at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.1.0:212)
        at com.mobileappscw3.nrcforums.HomeScreen$1.onDataChange(HomeScreen.java:76)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.1.0:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.1.0:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.1.0:55)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

其中 HomeScreen.java:76 是我的代码中的这一行:

String name = dataSnapshot.getValue(String.class);

我遵循的教程使用了字符串变量和参数,并且它有效。然而,我看过的这个问题的每个其他示例都使用 User 对象来检索数据 - 然而,由于我只存储了一个关于用户的属性(并且这个应用程序更多的是概念验证而不是真正的工作应用程序),我如果可能的话,宁愿不使用对象。有人可以帮忙吗?

最佳答案

更改此:

String name = dataSnapshot.getValue(String.class);

进入此:

String name = dataSnapshot.child("Name").getValue(String.class);

关于java - DatabaseException : Failed to convert value of type java. util.HashMap 到字符串 - Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55937406/

相关文章:

java - 使用 @Bean 注释和 Spring EL 表达式创建 Bean 和名称

java - 如何正确地将InputStream传递给另一个构造函数? ( java )

java - Android - 嵌套 fragment 和退出动画

java - 如何使用 DecimalFormat 方法更改小数点前数字的大小

java - 如何知道故障原因?

Android - SurfaceView(宽度,高度)问题

Android 12 启动画面不会动画

firebase - 错误:在此Director小部件上方找不到正确的Provider <User>

firebase - Auto Id 有通用格式吗?

swift - 快速从 firebase 数据库中读取子值