【问题标题】:Why is my application not catching any data from Firebase?为什么我的应用程序没有从 Firebase 捕获任何数据?
【发布时间】:2019-08-29 11:47:18
【问题描述】:

我正在登录屏幕。

但是,当我在填写完 ID 和 pw 后运行代码时,userId(下面的代码)仍然为空。

foundUser = dataSnapshot.getValue(User.class);
userId = foundUser.getId();
userPw = foundUser.getPw();

我的应用程序肯定已连接到 Firebase。但是,当我调试代码时,我可以看到 W/ClassMapper:在类 com.example.hellobook.User 上找不到 exampleuserID 的设置器/字段

【问题讨论】:

  • 请提供您的数据库结构以及为什么为数据库参考分配变量?
  • @Ashish 我的用户数据库包含布尔值 admin / firstName / id / lastName / numBookReports / pw 我刚刚分配了它,因为我认为它在某些程度上可能有用
  • 请张贴截图,以便我们正确理解问题。
  • @Ashish 我贴出来了

标签: java android firebase


【解决方案1】:

不应该这样

for (DataSnapshot ds : dataSnapshot.getChildren()) { 
    foundUser = dataSnapshot.getValue(User.class);

for (DataSnapshot ds : dataSnapshot.getChildren()) { 
    foundUser = ds.getValue(User.class);

【讨论】:

  • 好喝的酸奶!
  • 感谢您的关注,我修改了我的代码,但仍然无法正常工作:((
【解决方案2】:

您输入了错误的数据库参考。

DatabaseReference ref = database.getReference().child("hellobookdatabase").child("User").child(id);

现在你需要对你的 Condition 做些小改动。

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        foundUser = dataSnapshot.getValue(User.class);
        userId = foundUser.getId();
        userPw = foundUser.getPw();
        if (userId!=null && userId.equals(id)) {
            if (userPw.equals(psw)) {
                startActivity(new Intent(LoginScreen.this, LandingScreen.class));
            }
        }
        else {
            Toast.makeText(LoginScreen.this, "Wrong ID or password. Try again", Toast.LENGTH_SHORT).show();
        }

        }
    }
    @Override
    public void onCancelled (DatabaseError error) {
        @NonNull DatabaseError databaseError;                        }
});

您的查询不起作用的原因:

  1. 您的引用不正确。
  2. 您的 valueEventlistener 正在 hellobookdatabase 中搜索用户 ID,而不是 User 子级。

与此相关的任何其他查询只需评论即可。

【讨论】:

  • 非常感谢!!我认为它有帮助。但是,我仍然无法将我的代码从ref.addValueEventListener(new ValueEventListener() { 运行到Toast.makeText(LoginScreen.this, "Wrong ID or password. Try again", Toast.LENGTH_SHORT).show();。既不启动意图也不启动吐司。为什么会这样?
  • @purpleccherry 你能记录你试图匹配的 id 并提供你在 db 中使用的相同 id
  • 当我尝试使用正确的 id 和密码登录时,日志只显示D/EGL_emulation: eglMakeCurrent: 0xe9cdffc0: ver 3 0 (tinfo 0xe2510640),而这就是我所能看到的(没有编写其他代码)
  • @purplecherry 请加入stackoverflow chat room。这样评论区就不会乱了。
【解决方案3】:

试试 改变这个 DatabaseReference ref = database.getReference("User")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2023-01-13
    • 2018-08-03
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    相关资源
    最近更新 更多