Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法从Firebase(Android)获取数据_Java_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Java 无法从Firebase(Android)获取数据

Java 无法从Firebase(Android)获取数据,java,android,firebase,firebase-realtime-database,Java,Android,Firebase,Firebase Realtime Database,我试图从google firebase获取数据,但每次我都试图获取数据。“Else”案例每次都在运行。试图更改if(dataSnapshot.exists()),但收到相同的错误。请帮助解决它 public void retrieveProfileInfo() { currentUserID = mAuth.getUid(); key = rootRef.child("profiles").push().getKey(); rootRef.child("users-pro

我试图从google firebase获取数据,但每次我都试图获取数据。“Else”案例每次都在运行。试图更改if(dataSnapshot.exists()),但收到相同的错误。请帮助解决它

public void retrieveProfileInfo() {
    currentUserID = mAuth.getUid();
    key = rootRef.child("profiles").push().getKey();
    rootRef.child("users-profiles").child(currentUserID).child(key)
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    try {
                        if ((dataSnapshot.child("name").getValue()) != null){
                            String retrieveName = dataSnapshot.child("name").getValue().toString();
                            String retrieveStatus = dataSnapshot.child("about").getValue().toString();
                            nameBox.setText(retrieveName);
                            aboutBox.setText(retrieveStatus);
                        } else {
                            Toast.makeText(UserProfileActivity.this, currentUserID+"else - retrieveProfileInfo()",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }catch (Exception e){
                        Log.i("retrieveProfileInfo : ", "error is : " + e);
                        Toast.makeText(UserProfileActivity.this, e+" : Error",
                                Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

}

您不能按任意键并从中读取,您需要在
用户配置文件/userID
下遍历子项:

public void retrieveProfileInfo() {

//current user ID
currentUserID = mAuth.getUid();

ValueEventListener listener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

      for(DataSnapshot ds:dataSnapshot.getChildren()){

       //get the values
       String name = ds.child("name").getValue(String.class);
       String about = ds.child("about").getValue(String.class);
       String uid = ds.child("uid").getValue(String.class);


      }       
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // log error
        Log.w(TAG, "loadPost:onCancelled", databaseError.toException());

    }
};
rootRef.child("users-profiles").child(currentUserID).addValueEventListener(listener);


}