简体   繁体   中英

How to get data from current logged in user Firebase

I want to get all data from currently logged in user in my apps but when I get into the activity that supposed to show the data, the app stopped working. I have set up my Firebase correctly following the guide from Firebase website, I can login with authentication, save data into firebase, and also show all the data, but now i want to show only the data from currently logged in user.

Here is my activity that supposed to show the data

public class RsolverAccount extends AppCompatActivity {

    private FirebaseAuth firebaseAuth;
    TextView showEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rsolveraccount);

        setWidget();
        showData();
    }

    private void showData () {
        DatabaseReference mDatabase;
        mDatabase = FirebaseDatabase.getInstance().getReference().child("userdesc : "+ firebaseAuth.getCurrentUser().getUid());
        ValueEventListener postListener = new ValueEventListener() {

            @Override
            public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
                User user = dataSnapshot.getValue(User.class);
                showEmail.setText(user.getEmail());
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };
        mDatabase.addValueEventListener(postListener);
   }

    public void setWidget() {
        showEmail = (TextView) findViewById(R.id.showEmail);
    }
}

this is my Json Tree

sraboetapp 
    +description
    -userdesc
        -LquV8N1AJbRdhSKMA2T
            email: "bryan5@gmail.com"
            id: "870788464845"
            name: "bry5"
            pass: "123456"
            phone: "484854484546"

This is my logcat

2019-10-17 16:20:11.208 11014-11014/? I/ample.rsolveap: Not late-enabling -Xcheck:jni (already on)
2019-10-17 16:20:11.226 11014-11014/? E/ample.rsolveap: Unknown bits set in runtime_flags: 0x8000

2019-10-17 16:20:11.226 11014-11014/? W/ample.rsolveap: Unexpected CPU variant for X86 using defaults: x86

2019-10-17 16:20:11.583 11014-11049/com.example.rsolveapp W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.

2019-10-17 16:20:11.596 11014-11014/com.example.rsolveapp D/FirebaseAuth: Notifying id token listeners about user ( 0dkRQhKR1rQBcAUVYcULtI0C4zN2 ).

2019-10-17 16:20:11.598 11014-11014/com.example.rsolveapp D/FirebaseApp: com.google.firebase.iid.FirebaseInstanceId is not linked. Skipping initialization.

2019-10-17 16:20:11.598 11014-11014/com.example.rsolveapp D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.

2019-10-17 16:20:11.599 11014-11014/com.example.rsolveapp D/FirebaseApp: com.google.android.gms.measurement.AppMeasurement is not linked. Skipping initialization.

2019-10-17 16:20:11.599 11014-11014/com.example.rsolveapp I/FirebaseInitProvider: FirebaseApp initialization successful

2019-10-17 16:20:11.593 11014-11014/com.example.rsolveapp W/RenderThread: type=1400 audit(0.0:41): avc: denied { write } for name="property_service" dev="tmpfs" ino=6749 scontext=u:r:untrusted_app_27:s0:c130,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 app=com.example.rsolveapp

2019-10-17 16:20:11.608 11014-11056/com.example.rsolveapp D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.

2019-10-17 16:20:11.608 11014-11056/com.example.rsolveapp W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)

2019-10-17 16:20:11.619 11014-11056/com.example.rsolveapp D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so

2019-10-17 16:20:11.620 11014-11056/com.example.rsolveapp D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so

2019-10-17 16:20:11.622 11014-11056/com.example.rsolveapp D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so

2019-10-17 16:20:11.639 11014-11051/com.example.rsolveapp W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.

2019-10-17 16:20:11.646 11014-11051/com.example.rsolveapp I/FirebaseAuth: [FirebaseAuth:] Loading module via FirebaseOptions.

2019-10-17 16:20:11.646 11014-11051/com.example.rsolveapp I/FirebaseAuth: [FirebaseAuth:] Preparing to create service connection to gms implementation

2019-10-17 16:20:11.673 11014-11014/com.example.rsolveapp W/ample.rsolveap: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)

2019-10-17 16:20:11.673 11014-11014/com.example.rsolveapp W/ample.rsolveap: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)

2019-10-17 16:20:11.728 11014-11014/com.example.rsolveapp D/FirebaseApp: Notifying auth state listeners.

2019-10-17 16:20:11.728 11014-11014/com.example.rsolveapp D/FirebaseApp: Notified 0 auth state listeners.

2019-10-17 16:20:11.750 11014-11054/com.example.rsolveapp W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...

2019-10-17 16:20:11.752 11014-11054/com.example.rsolveapp D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0

2019-10-17 16:20:11.752 11014-11054/com.example.rsolveapp D/EGL_emulation: eglCreateContext: 0xe151a180: maj 3 min 1 rcv 4

2019-10-17 16:20:11.769 11014-11054/com.example.rsolveapp D/EGL_emulation: eglMakeCurrent: 0xe151a180: ver 3 1 (tinfo 0xe150f480)

2019-10-17 16:20:11.771 11014-11054/com.example.rsolveapp E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da

2019-10-17 16:20:11.771 11014-11054/com.example.rsolveapp E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da

2019-10-17 16:20:11.782 11014-11054/com.example.rsolveapp W/Gralloc3: mapper 3.x is not supported

2019-10-17 16:20:11.785 11014-11054/com.example.rsolveapp D/eglCodecCommon: allocate: Ask for block of size 0x1000

2019-10-17 16:20:11.785 11014-11054/com.example.rsolveapp D/eglCodecCommon: allocate: ioctl allocate returned offset 0x3ff807000 size 0x2000

2019-10-17 16:20:11.794 11014-11054/com.example.rsolveapp D/EGL_emulation: eglMakeCurrent: 0xe151a180: ver 3 1 (tinfo 0xe150f480)

2019-10-17 16:20:11.813 11014-11054/com.example.rsolveapp D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 1 0

This is my User class

package com.example.rsolveapp;

import com.google.firebase.database.IgnoreExtraProperties;

import java.io.Serializable;

public class User implements Serializable{
private String email;
private String name, pass, id, phone;
private String key;

public User(){

}

public String getKey(){
    return key;
}

public void setKey(String key){
    this.key=key;
}

public String getEmail(){
    return email;
}

public void setEmail(String email){
    this.email=email;
}

public String getName(){
    return name;
}

public void setName(String name){
    this.name=name;
}

public String getPass(){
    return pass;
}

public void setPass(String pass){
    this.pass=pass;
}

public String getId(){
    return id;
}

public void setId(String id){
    this.id=id;
}

public String getPhone(){
    return phone;
}

public void setPhone(String phone){
    this.phone=phone;
}

@Override
public String toString(){
    return " "+email+"/n"+
            " "+name+"/n"+
            " "+pass+"/n"+
            " "+id+"/n"+
            " "+phone;
}

public User(String Remail, String Rname, String Rpass, String Rid, String Rphone){
    email=Remail;
    name=Rname;
    pass=Rpass;
    id=Rid;
    phone=Rphone;
}
}

EDIT: this is my activity right now

public class RsolverAccount extends AppCompatActivity {


TextView showEmail;
private static final String TAG = RsolverAccount.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rsolveraccount);

    setWidget();
    showData();
}

private void showData () {

    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    if(firebaseUser != null) {
        String email = firebaseUser.getEmail();
        Log.d(TAG, email);
    }

    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
    DatabaseReference mDatabase;
    mDatabase = FirebaseDatabase.getInstance().getReference().child("sraboetapp").child("userdesc").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
    ValueEventListener postListener = new ValueEventListener() {

        @Override
        public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
            User user = dataSnapshot.getValue(User.class);
            showEmail.setText(user.getEmail());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };
    mDatabase.addValueEventListener(postListener);
}

public void setWidget() {
    showEmail = (TextView) findViewById(R.id.showEmail);
}
}

This is the error i get

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rsolveapp, PID: 13698
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.rsolveapp.User.getEmail()' on a null object reference
    at com.example.rsolveapp.RsolverAccount$1.onDataChange(RsolverAccount.java:45)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75)
    at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Your query might be wrong according to your database structure:

Try using this:

mDatabase = FirebaseDatabase.getInstance().getReference().child("sraboetapp").child("userdesc").child(FirebaseAuth.getInstance().getCurrentUser().getUid());

Instead of:

mDatabase = FirebaseDatabase.getInstance().getReference().child("userdesc : "+ firebaseAuth.getCurrentUser().getUid());

Your query searching for child with user and id together which is not available in your database and i don't know if sraboetapp is base node or just application name so i put it.

According to your comment, you are getting the following error:

Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()'

Because your firebaseAuth object is null . You did not initialize it. To solve this, simply add the following line of code:

FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();

As the first line in your showData() method.

Edit:

FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if(firebaseUser != null) {
    String email = firebaseUser.getEmail();
    Log.d(TAG, email);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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