Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.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
Android 应用程序将数据更新到firstore,但不';设置所需的UID_Android_Firebase_Google Cloud Firestore_Firebase Authentication - Fatal编程技术网

Android 应用程序将数据更新到firstore,但不';设置所需的UID

Android 应用程序将数据更新到firstore,但不';设置所需的UID,android,firebase,google-cloud-firestore,firebase-authentication,Android,Firebase,Google Cloud Firestore,Firebase Authentication,当我运行我的程序时,我没有看到任何错误。目前我正在尝试对我的应用程序使用firebase的电话身份验证。当我尝试将数据保存到FirStore时,它会上载数据,但不会设置所需的UId。我想首先检查用户是否已登录,如果他是,他将被引导到details.activity,如果不是,他将被引导到details.activity必须在register.activity中注册。我要设置UID来代替用户ID RegisterActivity.java public class RegisterActivit

当我运行我的程序时,我没有看到任何错误。目前我正在尝试对我的应用程序使用firebase的电话身份验证。当我尝试将数据保存到FirStore时,它会上载数据,但不会设置所需的UId。我想首先检查用户是否已登录,如果他是,他将被引导到details.activity,如果不是,他将被引导到details.activity必须在register.activity中注册。我要设置UID来代替用户ID

RegisterActivity.java

 public class RegisterActivity extends AppCompatActivity {
    FirebaseAuth firebaseAuth;
    FirebaseFirestore firestore;

     EditText phoneNumber;
    EditText codeEnter;
    Button NextButton;
    ProgressBar progressBar;
    TextView State;
    CountryCodePicker codePicker;
    String TAG ="MyTag";
    String verificationID;
    PhoneAuthProvider.ForceResendingToken Token;
    Boolean verifiacationInProgress=false;




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

        firebaseAuth= FirebaseAuth.getInstance();
        firestore=FirebaseFirestore.getInstance();

        phoneNumber= findViewById(R.id.phone);
        codeEnter=findViewById(R.id.codeEnter);
        NextButton=findViewById(R.id.nextButton);
        progressBar=findViewById(R.id.progressBar);
        State=findViewById(R.id.state);
        codePicker=findViewById(R.id.ccp);



        NextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!verifiacationInProgress){
                    if (!phoneNumber.getText().toString().isEmpty() && phoneNumber.getText().toString().length() == 10) {
                        String phnNumber = "+" + codePicker.getSelectedCountryCode() + phoneNumber.getText().toString();
                        Log.d(TAG, "Phone Number is   " + phnNumber);
                        progressBar.setVisibility(View.VISIBLE);
                        State.setText("SENDING OTP");
                        State.setVisibility(View.VISIBLE);
                        requestOTP(phnNumber);


                    } else {
                        phoneNumber.setError("Phone number is not valid");
                    }
            }
                else {
                    String UserOTP=codeEnter.getText().toString();
                    if (!UserOTP.isEmpty()&&UserOTP.length()==6){
                        PhoneAuthCredential credential=PhoneAuthProvider.getCredential(verificationID,UserOTP);
                        VerifyAuth(credential);

                    }
                    else {
                        codeEnter.setError("Invalid OTP");
                    }
                }
            }
        });




    }
    @Override
    protected void onStart() {
        super.onStart();
        if(firebaseAuth!=null){
            progressBar.setVisibility(View.VISIBLE);
            State.setText("Checking");
            State.setVisibility(View.VISIBLE);
            CheckUserProfile();
        }
    }

    private void VerifyAuth(PhoneAuthCredential credential) {
        firebaseAuth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if(task.isSuccessful()){
                    CheckUserProfile();

                }
                else {
                    Toast.makeText(RegisterActivity.this,"Login Failed",Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    private void CheckUserProfile(){
        DocumentReference documentReference=firestore.collection("users")
                .document(firebaseAuth.getCurrentUser().getUid());
        documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                if(documentSnapshot.exists()){
                    Intent intent=new Intent(RegisterActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }
                else {
                    Intent intent = new Intent(RegisterActivity.this,AddDetails.class);
                    startActivity(intent);
                    finish();
                }

            }
        });

    }

    private void requestOTP(String phnNumber) {
        PhoneAuthProvider.getInstance().verifyPhoneNumber(phnNumber, 60L, TimeUnit.SECONDS, RegisterActivity.this,
                new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
                    @Override
                    public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
                        super.onCodeSent(s, forceResendingToken);
                        verificationID = s;
                        Token= forceResendingToken;
                        progressBar.setVisibility(View.INVISIBLE);
                        State.setVisibility(View.INVISIBLE);
                        codeEnter.setVisibility(View.VISIBLE);
                        NextButton.setText("Verify");
                        NextButton.setEnabled(true);
                        verifiacationInProgress=true;



                    }

                    @Override
                    public void onCodeAutoRetrievalTimeOut(@NonNull String s) {
                        super.onCodeAutoRetrievalTimeOut(s);
                    }

                    @Override
                    public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {

                    }

                    @Override
                    public void onVerificationFailed(@NonNull FirebaseException e) {
                        Toast.makeText(RegisterActivity.this,"Cannot create account"+e.getMessage(),Toast.LENGTH_SHORT).show();

                    }


                });
    }
}
公共类注册表活动扩展了AppCompatActivity{
FirebaseAuth FirebaseAuth;
FirebaseFirestore firestore;
编辑文本电话号码;
编辑文本代码输入;
按钮下一个按钮;
ProgressBar ProgressBar;
文本视图状态;
CountryCodePicker代码选择器;
String TAG=“MyTag”;
字符串验证ID;
PhoneAuthProvider.ForceResendingToken令牌;
Boolean VerificationInProgress=false;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u寄存器);
firebaseAuth=firebaseAuth.getInstance();
firestore=FirebaseFirestore.getInstance();
phoneNumber=findViewById(R.id.phone);
codenter=findviewbyd(R.id.codenter);
NextButton=findviewbyd(R.id.NextButton);
progressBar=findViewById(R.id.progressBar);
State=findviewbyd(R.id.State);
代码选择器=findviewbyd(R.id.ccp);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(!VerificationInProgress){
如果(!phoneNumber.getText().toString().isEmpty()&&phoneNumber.getText().toString().length()==10){
字符串phnNumber=“+”+codePicker.getSelectedCountryCode()+phoneNumber.getText().toString();
Log.d(标签,“电话号码为”+phnNumber);
progressBar.setVisibility(View.VISIBLE);
State.setText(“发送OTP”);
State.setVisibility(View.VISIBLE);
请求OTP(phnNumber);
}否则{
电话号码。设置错误(“电话号码无效”);
}
}
否则{
字符串UserOTP=codenter.getText().toString();
如果(!UserOTP.isEmpty()&&UserOTP.length()==6){
PhoneAuthCredential credential=PhoneAuthProvider.getCredential(verificationID,UserOTP);
验证授权(凭证);
}
否则{
codenter.setError(“无效OTP”);
}
}
}
});
}
@凌驾
受保护的void onStart(){
super.onStart();
如果(firebaseAuth!=null){
progressBar.setVisibility(View.VISIBLE);
State.setText(“检查”);
State.setVisibility(View.VISIBLE);
CheckUserProfile();
}
}
私有void VerifyAuth(PhoneAuthCredential凭据){

firebaseAuth.signInWithCredential(credential).addOnCompleteListener(新OnCompleteListener

您正在使用的
final DocumentReference DocumentReference=firebaseFirestore.collection(“users”).document(“UserID”);
将文档名称设置为
UserID
。您可以指定一个viarble以在其中放入特定文本(例如用户身份验证uid)或您可以使用空的
.document();
创建具有自动生成ID的文档引用。您可以查看更多信息。

您使用的是
最终文档引用DocumentReference=firebaseFirestore.collection(“用户”).document(“用户ID”);
将文档名称设置为
UserId
。您可以指定一个viarble以在其中放入特定文本(例如用户身份验证uid),也可以使用空的
。document();
使用自动生成的ID创建文档引用。您可以查看更多信息。

创建文档后,您不能更改文档的名称。要使用用户ID命名集合,您必须在创建集合时提供名称。 大概是这样的:

String userId = firebaseAuth.getCurrentUser().getUid();
Map<String,Object> user=new HashMap<>();
                    user.put("first",firstName);
                    user.put("last",lastName);
                    user.put("EmailID",emailId);
firebaseFirestore.collection("users").document(userId).set(user)
String userId=firebaseAuth.getCurrentUser().getUid();
Map user=newhashmap();
user.put(“first”,firstName);
user.put(“last”,lastName);
user.put(“EmailID”,EmailID);
firebaseFirestore.collection(“用户”).document(userId).set(用户)

您不能在文档创建后更改其名称。要使用用户id命名集合,您必须在创建集合时提供名称。 大概是这样的:

String userId = firebaseAuth.getCurrentUser().getUid();
Map<String,Object> user=new HashMap<>();
                    user.put("first",firstName);
                    user.put("last",lastName);
                    user.put("EmailID",emailId);
firebaseFirestore.collection("users").document(userId).set(user)
String userId=firebaseAuth.getCurrentUser().getUid();
Map user=newhashmap();
user.put(“first”,firstName);
user.put(“last”,lastName);
user.put(“EmailID”,EmailID);
firebaseFirestore.collection(“用户”).document(userId).set(用户)
    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AddDetails">

    <EditText
        android:id="@+id/FirstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="96dp"
        android:layout_marginTop="78dp"
        android:ems="10"
        android:hint="First Name"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/LastName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="96dp"
        android:layout_marginTop="61dp"
        android:ems="10"
        android:hint="Last Name"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/FirstName" />

    <EditText
        android:id="@+id/email_address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="96dp"
        android:layout_marginTop="74dp"
        android:ems="10"
        android:hint="Email Address"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/LastName" />

    <Button
        android:id="@+id/save_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="110dp"
        android:text="Save Data"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/email_address" />
</androidx.constraintlayout.widget.ConstraintLayout>
String userId = firebaseAuth.getCurrentUser().getUid();
Map<String,Object> user=new HashMap<>();
                    user.put("first",firstName);
                    user.put("last",lastName);
                    user.put("EmailID",emailId);
firebaseFirestore.collection("users").document(userId).set(user)