从firebase数据库接收数字数据

drkbr07n  于 2021-07-05  发布在  Java
关注(0)|答案(1)|浏览(292)

我想在文本视图中显示分数的数值,但应用程序总是关闭。
这是我的密码

FirebaseDatabase database = FirebaseDatabase.getInstance();
        FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        DatabaseReference myRef = database.getReference("users").child(currentFirebaseUser.getUid());

        // Read from the database
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String valuePoints = dataSnapshot.child("current_points").getValue(String.class);
                mTextViewPoints = findViewById(R.id.current_points);
                mTextViewPoints.setText(valuePoints);
            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                // Log.w(TAG, "Failed to read value.", error.toException());
            }
        });
    }```

Other values that contain letters are numbers he displays, but when it comes only to numbers he closes.
Where am I going wrong?
798qvoo8

798qvoo81#

亲爱的vanderclin rocha,
最初,不能在回调valueeventlistener中定义textview,应该在视图启动时定义您。
您的代码textview中的问题返回null,您可以进行调试并查看它。这是当前获取数据并将其设置为textview的方法。

FirebaseDatabase database = FirebaseDatabase.getInstance();
    FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    DatabaseReference myRef = database.getReference("users").child(currentFirebaseUser.getUid());

textview mtextViews=findviewbyid(r.id.当前\u点);

// Read from the database
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String valuePoints = dataSnapshot.child("current_points").getValue(String.class);
            mTextViewPoints.setText(valuePoints);
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            // Log.w(TAG, "Failed to read value.", error.toException());
        }
    });
}

我希望这对你有帮助

相关问题