简体   繁体   English

从Firebase数据库读取特定的子值

[英]Read a particular child value from firebase database

I am trying to retrieve value(phone number) for a child from my firebase realtime database. 我正在尝试从Firebase实时数据库中检索孩子的值(电话号码)。 However it gives me the following error: 但是,它给了我以下错误:

Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference 尝试在空对象引用上调用虚拟方法'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)'

The structure of my database looks like this: 我的数据库结构如下:

contactsfirebase-16ade
  users
    -LiKtfTuDQm7LmY49t69
      email: "devsingh0@gmail.com"
      name: "dev"
      phone: "2327272"

Code

DatabaseReference databaseReference;
databaseReference.child("users").child("userID").child("phone").addListenerForSingleValueEvent(new ValueEventListener()
{
   @Override
   public void onDataChange(@NonNull DataSnapshot dataSnapshot) 
    {
     String phone = dataSnapshot.getValue().toString();

     Phone.setText(phone);
     Phone.getDisplay();
    }
}

Read child value from firebase 从Firebase读取子值

I want the value "2327272" to be shown in the textview Phone on my app. 我希望在应用程序的textview手机中显示值“ 2327272”。 However when I click the button it gives the error as shared above and the app crashes. 但是,当我单击按钮时,它给出了上面共享的错误,并且应用程序崩溃了。

Your DatabaseReference databaseReference; 您的DatabaseReference databaseReference; is not initialized 未初始化

Do this 做这个

DatabaseReference databaseReference; 
databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.child("users").child("userID").child("phone").addListenerForSingleValueEvent...

You can try this method : 您可以尝试以下方法:

 @Override
        protected void onStart() {
            super.onStart();
            final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
//if you retrive the Uid inside of oncreate method you can call it else you have to retrive the Uid
            DatabaseReference veera = rootRef.child("users").child(FirebaseAuth.getInstance().getCurrentUser().getUid();
            ValueEventListener eventListener = new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if(dataSnapshot.child("phone").exits()){
                       String phone = dataSnapshot.getValue().toString();
                        Phone.setText(phone);
                  }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            };
            veera.addValueEventListener(eventListener);

        }

If you are doing in under a button click just move it to a public method and call the method inside of onclicklistener. 如果要在按钮下进行操作,只需将其移至公共方法并在onclicklistener内调用该方法。

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
          //here
      }
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM