本文介绍了Android Firebase - 无法使用 getUid() 获取用户 ID - 错误:在空对象引用上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Firebase 并且注册/登录活动工作正常,但我也希望每个用户更新用户名.好像跑不起来了:

I am using Firebase and have a signup / login activities working fine but I also want each user to update a username.It seems I am unable to run:

firebaseRef.getAuth().getUid()

我在应用模拟器中收到错误消息不幸的是,应用已停止".

I am receiving an error of within the app emulator "unfortunately, app has stopped".

这看起来像是我在监视器中的 android studio 中遇到的主要错误:与 getUid() 有关.

This looks like the main error I am getting in android studio in the monitor: Something to do with the getUid().

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法java.lang.String com.firebase.client.AuthData.getUid()"

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.firebase.client.AuthData.getUid()' on a null object reference

这是我的活动代码:

package me.netsync.netconnect;

import android.app.ActionBar;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.firebase.client.AuthData;
import com.firebase.client.Firebase;

public class SettingActivity extends AppCompatActivity {

    private Firebase mRef;
    private Button BtnSettings;
    private EditText TxtUsername;

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

        mRef = new Firebase(Constants.FIREBASE_URL);

        BtnSettings = (Button)findViewById(R.id.BtnSettings);
        BtnSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // username txt update the profile
                TxtUsername = (EditText)findViewById(R.id.TxtUsername);
                String TxtUsernameStr = TxtUsername.getText().toString();
                TxtUsernameStr = TxtUsernameStr.trim();

                // check we can get a userid
               /* if(mRef.getAuth() == null){
                    Intent intent = new Intent(SettingActivity.this,LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                }*/

                if(TxtUsernameStr.isEmpty()){
                    // show a dialog box
                    AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                    builder.setMessage("Please make sure you enter a Username" + TxtUsernameStr)
                            .setTitle("Error!")
                            .setPositiveButton(android.R.string.ok, null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }else{
                    Firebase upDateUserProfile = new Firebase(Constants.FIREBASE_URL);
                    String uid = upDateUserProfile.getAuth().getUid();
                    User user = new User(TxtUsernameStr);
                    upDateUserProfile.child("users").child(uid).child("username").setValue(user);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        int btn = item.getItemId();

        if(btn == R.id.action_logout){
            mRef.unauth();
            Intent intent = new Intent(this,LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }

        if(btn == R.id.action_settings){
            mRef.unauth();
            Intent intent = new Intent(this,SettingActivity.class);
            startActivity(intent);
        }

        return true;
    }
}

任何指针表示赞赏.

推荐答案

在您的应用 build.gradle 中,将此添加到您的依赖项

In your app build.gradle, add this to your dependencies

编译'com.google.firebase:firebase-auth:9.4.0'

compile 'com.google.firebase:firebase-auth:9.4.0'

import com.google.firebase.auth.FirebaseAuth;
...
...

if (FirebaseAuth.getInstance().getCurrentUser() == null) {
    //Go to login
}
else{
    String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
}

这篇关于Android Firebase - 无法使用 getUid() 获取用户 ID - 错误:在空对象引用上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 21:21