gpt4 book ai didi

java.lang.NullPointerException : Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()'

转载 作者:行者123 更新时间:2023-12-01 17:55:48 45 4
gpt4 key购买 nike

Process: com.example.vicevirus.myapplication, PID: 13471  
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.example.vicevirus.myapplication.RegisterActivity$1.onComplete(RegisterActivity.java:48)
at com.google.android.gms.tasks.zzc$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

这是在 Android 显示器上。我该如何解决这个问题?

package com.example.vicevirus.myapplication;

import android.app.ProgressDialog;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class RegisterActivity extends AppCompatActivity {
private EditText email,name,password;
DatabaseReference databaseReference;
FirebaseAuth mAuth;
private DatabaseReference userIdRef;
ProgressDialog registerDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
email=(EditText)findViewById(R.id.email);
name=(EditText)findViewById(R.id.name);
password=(EditText)findViewById(R.id.password);
databaseReference= FirebaseDatabase.getInstance().getReference().child("chat_users");
mAuth=FirebaseAuth.getInstance();
registerDialog=new ProgressDialog(this);
registerDialog.setMessage("Registering..");
}
public void submit(View view) {
registerDialog.show();
mAuth.createUserWithEmailAndPassword(email.getText().toString(),password.getText().toString()).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isComplete())
{
registerDialog.dismiss();
Toast.makeText(getApplicationContext(),"Registered successfully",Toast.LENGTH_SHORT).show();
userIdRef=databaseReference.child(mAuth.getCurrentUser().getUid());
userIdRef.child("name").setValue(name.getText().toString());
finish();
}
registerDialog.dismiss();
}
});
}
}

最佳答案

问题似乎是您的用户尚未登录甚至尚未注册。因此调用 mAuth.getCurrentUser() 返回 null。

查看this page of the FireBase documentation关于如何实现用户注册。这可能是失败的。需要检查 task.isSuccessful() 来检查这一点。

关于java.lang.NullPointerException : Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44979970/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com