2017-10-11 47 views
-1

我正在为使用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) 
+1

后错误日志获取用户对象.. –

+0

@DivyeshPatel我没有 – Bogle

回答

1

您启动新mUser对象。

mUser = new UserInformation(); 

然后试图从它得到getPhone_num()但它没有电话号码。

mUser.getPhone_num(); 

你要通过FirebaseUser user的意图,同时启动第二个活动,然后从getIntent()

+0

我很抱歉,我没有补充说文件但我创建了另一个带有用户信息的类 – Bogle