当authen时,Firebase .getUID NullPointerException

时间:2017-08-01 21:56:57

标签: android firebase firebase-realtime-database firebase-authentication

我在注册时会向数据库添加数据,但是我有

Java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()'

我尝试了一些东西,但是没有工作

这是我的代码

    auth = FirebaseAuth.getInstance();

    database = FirebaseDatabase.getInstance();

    mRef = database.getReference();
    FirebaseUser user = auth.getCurrentUser();
    userID = user.getUid();
btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String fname = etFname.getText().toString().trim();
            final String lname = etLname.getText().toString().trim();
            final String id_card = etIdCard.getText().toString().trim();
            final String email = etEmail.getText().toString().trim();
            final String mobile = etMobile.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            if (TextUtils.isEmpty(email)){
                Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(password)){
                Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (TextUtils.isEmpty(fname)){
                Toast.makeText(getApplicationContext(), "Enter FirstName!", Toast.LENGTH_SHORT).show();
                return;
            }

            if (password.length() < 8) {
                Toast.makeText(getApplicationContext(), "Password too short, enter minimum 8 characters!", Toast.LENGTH_SHORT).show();
                return;
            }

            progressBar.setVisibility(View.VISIBLE);

            auth.createUserWithEmailAndPassword(email,password)
                    .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {


                            if (!task.isSuccessful()) {

                                UserInfo userInfo = new UserInfo(fname,lname,id_card,mobile,email);
                                mRef.child("users").child(userID).setValue(userInfo);
                                etFname.setText("");
                                etLname.setText("");
                                etIdCard.setText("");
                                etMobile.setText("");
                                etEmail.setText("");
                                Toast.makeText(RegisterActivity.this, "Authentication failed." + task.getException(),
                                        Toast.LENGTH_SHORT).show();

                            } else {
                                Toast.makeText(RegisterActivity.this, "Register Successfully" + task.isSuccessful(), Toast.LENGTH_SHORT).show();
                                progressBar.setVisibility(View.GONE);

                                finish();
                            }

                        }
                    });
        }
    });


}

现在得到这样的新错误 java.lang.NoSuchMethodError:类Lcom / google / firebase / FirebaseApp中没有虚方法zzTt()Z;或其超级类(“com.google.firebase.FirebaseApp”的声明出现在/data/app/com.example.androiddev.army31-1/split_lib_dependencies_apk.apk:classes36.dex)

现在我可以修复它,因为在gradle上编译不是同一个版本

并得到同样的东西

enter image description here

3 个答案:

答案 0 :(得分:2)

您在代码中提到的auth类型是什么,将其替换为单行并检查它是否有效

        String mUid = FirebaseAuth.getInstance().getCurrentUser().getUid();

您还可以查看文档以获取完整的代码示例:https://firebase.google.com/docs/auth/android/manage-users

答案 1 :(得分:2)

检查一下:

if(auth.getCurrentUser() != null){
    uid = auth.getCurrentUser().getUID();
}

答案 2 :(得分:-1)

public class App extends Application {
@Override
public void onCreate() {
    super.onCreate();

    OneSignal.startInit(this)
            .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
            .setNotificationOpenedHandler(new MainNotificationOpenedHandler())
            .setNotificationReceivedHandler(new MainNotificationReceivedHandler())
            .unsubscribeWhenNotificationsAreDisabled(true)
            .init();

    OSPermissionSubscriptionState status = OneSignal.getPermissionSubscriptionState();
}

private class MainNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
    // This fires when a notification is opened by tapping on it.
    @Override
    public void notificationOpened(OSNotificationOpenResult result) {
        OSNotificationAction.ActionType actionType = result.action.type;
        JSONObject data = result.notification.payload.additionalData;
        String customKey;

        Core.coreView.loadUrl(result.notification.payload.launchURL);
    }
}

private class MainNotificationReceivedHandler implements OneSignal.NotificationReceivedHandler {
    @Override
    public void notificationReceived(OSNotification notification) {
        JSONObject data = notification.payload.additionalData;
    }

}

要运行以上两行,您必须(我强调您必须)在Firebase中进行基于身份验证的用户名/密码。它正在抛出 FirebaseUser user = auth.getCurrentUser(); userID = user.getUid(); ,因为大多数新手程序员忘记在Firebase控制台中设置基于用户的身份验证。

您可以在Firebase身份验证中添加用户。

如果您使用 Firebase实时数据库,我建议您在“规则”标签中添加以下规则。

NullPointerException

}