我正在爲使用Firebase的android studio進行電話Auth登錄,我的java和XML代碼適用於第一個活動,但是當我嘗試分解它以使其使用第二個活動時它崩潰。Firebase手機身份驗證分爲兩個活動
第一項活動是用於接收用戶的號碼,一旦用戶輸入號碼並按下「發送」,用戶將被帶到驗證授權碼並重新發送代碼的第二活動,我可以獲得代碼從第一頁發送,但當我進入第二頁並輸入時,應用程序崩潰。當我嘗試調試時,我在兩個編輯文本字段中都獲得了空值。
public class PhoneAuthActivity extends AppCompatActivity implements
View.OnClickListener {
private static final String TAG = "PhoneAuthActivity";
private UserInformation mUser;
EditText mVerificationField,mPhoneNumberField;
Button mStartButton;
private FirebaseAuth mAuth;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in2);
mUser = new UserInformation();
// Assign views
mPhoneNumberField = (EditText) findViewById(R.id.PhoneNumber);
mStartButton = (Button) findViewById(R.id.Start_Verification);
mStartButton.setOnClickListener(this);
mAuth = FirebaseAuth.getInstance();
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
Log.d(TAG, "onVerificationCompleted:" + credential);
signInWithPhoneAuthCredential(credential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
Log.w(TAG, "onVerificationFailed", e);
if (e instanceof FirebaseAuthInvalidCredentialsException) {
mPhoneNumberField.setError("Invalid phone number.");
} else if (e instanceof FirebaseTooManyRequestsException) {
Toast.makeText(PhoneAuthActivity.this, "Quota exceeded", Toast.LENGTH_LONG).show();
}
}
};
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = task.getResult().getUser();
startActivity(new Intent(PhoneAuthActivity.this, Name_Activity.class));
finish();
} else {
Log.w(TAG, "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
mVerificationField.setError("Invalid code.");
}
}
}
});
}
private void verifyPhoneNumberWithCode(String verificationId, String code) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
//signInWithPhoneAuthCredential(credential);
}
private void startPhoneNumberVerification(String phoneNumber) {
mUser.setPhone_num(phoneNumber);
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
}
private boolean validatePhoneNumber() {
String phoneNumber = mPhoneNumberField.getText().toString();
if (TextUtils.isEmpty(phoneNumber)) {
mPhoneNumberField.setError("Invalid phone number.");
return false;
}
return true;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.Start_Verification:
if (!validatePhoneNumber()) {
return;
}
startPhoneNumberVerification(mPhoneNumberField.getText().toString());
startActivity(new Intent(PhoneAuthActivity.this,
PhoneVerAuthActivity.class));
finish();
break;
}
}
}
第二活動
public class PhoneVerAuthActivity extends AppCompatActivity implements
View.OnClickListener {
private static final String TAG = "PhoneAuthActivity";
String phone;
EditText mVerificationField,mPhoneNumberField;
Button mVerifyButton, mResendButton;
String mVerificationId;
private FirebaseAuth mAuth;
private PhoneAuthProvider.ForceResendingToken mResendToken;
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
private UserInformation mUser;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in3);
mUser = new UserInformation();
// Assign views
mVerificationField = (EditText) findViewById(R.id.NumVer);
mVerifyButton = (Button) findViewById(R.id.btnVerify);
mResendButton = (Button) findViewById(R.id.btnresend);
mVerifyButton.setOnClickListener(this);
mResendButton.setOnClickListener(this);
mAuth = FirebaseAuth.getInstance();
phone = mUser.getPhone_num();
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(PhoneAuthCredential credential) {
Log.d(TAG, "onVerificationCompleted:" + credential);
signInWithPhoneAuthCredential(credential);
}
@Override
public void onVerificationFailed(FirebaseException e) {
Log.w(TAG, "onVerificationFailed", e);
if (e instanceof FirebaseAuthInvalidCredentialsException) {
mPhoneNumberField.setError("Invalid phone number.");
} else if (e instanceof FirebaseTooManyRequestsException) {
Toast.makeText(PhoneVerAuthActivity.this, R.string.Quota_exceeded, Toast.LENGTH_LONG).show();
}
}
@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token) {
Log.d(TAG, "onCodeSent:" + verificationId);
mVerificationId = verificationId;
mResendToken = token;
}
};
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = task.getResult().getUser();
startActivity(new Intent(PhoneVerAuthActivity.this, Name_Activity.class));
finish();
} else {
Log.w(TAG, "signInWithCredential:failure", task.getException());
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
mVerificationField.setError(getString(R.string.Invalid_code));
}
}
}
});
}
private void verifyPhoneNumberWithCode(String verificationId, String code) {
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
signInWithPhoneAuthCredential(credential);
}
private void resendVerificationCode(String phoneNumber,
PhoneAuthProvider.ForceResendingToken token) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks, // OnVerificationStateChangedCallbacks
token); // ForceResendingToken from callbacks
}
private boolean validatePhoneNumber() {
String phoneNumber = mVerificationField.getText().toString();
if (TextUtils.isEmpty(phoneNumber)) {
mVerificationField.setError(getString(R.string.Invalid_phonenumber));
return false;
}
return true;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnVerify:
String code = mVerificationField.getText().toString();
if (TextUtils.isEmpty(code)) {
mVerificationField.setError(getString(R.string.cannot_be_empty));
return;
}
verifyPhoneNumberWithCode(mVerificationId, code);
break;
case R.id.btnresend:
resendVerificationCode(phone, mResendToken);
break;
}
}
}
UserInformation
public class UserInformation {
private String name;
private String phone_num;
public UserInformation()
{
}
public UserInformation (String name)
{
this.name = name;
//this.phone_num = phone_num
}
public String getPhone_num(){
return phone_num;
}
public void setPhone_num(String phone_num){
this.phone_num = phone_num;
}
public String getName() { return name; }
public void setName(String name) { this.name = name;}
}
Process: com.example.bogle.chatdemo2, PID: 32609
java.lang.IllegalArgumentException: Given String is empty or null
at com.google.android.gms.common.internal.zzbo.zzcF(Unknown Source)
at com.google.firebase.auth.PhoneAuthCredential.<init>(Unknown Source)
at com.google.firebase.auth.PhoneAuthProvider.getCredential(Unknown Source)
at com.example.bogle.chatdemo2.PhoneVerAuthActivity.verifyPhoneNumberWithCode(PhoneVerAuthActivity.java:127)
at com.example.bogle.chatdemo2.PhoneVerAuthActivity.onClick(PhoneVerAuthActivity.java:181)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at android.view.View$PerformClick.run(View.java:23653)
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:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
後錯誤日誌獲取用戶對象.. –
@DivyeshPatel我沒有 – Bogle