java - 使用 Firebase 电话身份验证验证 OTP 后应用程序崩溃
问题描述
编辑:已经解决了。
这个问题与之前的问题不同。我 在 android 中使用 firebase phone Auth 时提到了应用程序崩溃,但我的应用程序仍然崩溃。
因此,我正在尝试设计一个聊天应用程序,用户可以在其中使用 Firebase 提供的 OTP 登录,但是当我输入手机上收到的 OTP(正确/错误)时,应用程序崩溃。如果有人可以请指导我,那就太棒了。
注意:我已经包含了实现“androidx.browser:browser:1.3.0”依赖,但是当我尝试验证 OTP 时应用程序仍然崩溃
我得到的主要错误是
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.commutify, PID: 5168
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
at com.example.commutify.otpAuthentication$2.onClick(otpAuthentication.java:61)
at android.view.View.performClick(View.java:6647)
at android.view.View.performClickInternal(View.java:6620)
at android.view.View.access$3100(View.java:791)
at android.view.View$PerformClick.run(View.java:26346)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7081)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:928)
所以这是我的代码,其中OTP 身份验证发生
TextView mchangenumber;
EditText mgetotp;
android.widget.Button mverifyotp;
String enteredotp;
FirebaseAuth firebaseAuth;
ProgressBar mprogressbarofotpauth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_otp_authentication);
mchangenumber = findViewById(R.id.changenumber);
mverifyotp = findViewById(R.id.verifyotp);
mgetotp = findViewById(R.id.getotp);
mprogressbarofotpauth = findViewById(R.id.progressbarofmain);
firebaseAuth = FirebaseAuth.getInstance();
mchangenumber.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(otpAuthentication.this, MainActivity.class);
startActivity(intent);
}
});
mverifyotp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
enteredotp = mgetotp.getText().toString();
if(enteredotp.isEmpty()){
Toast.makeText(getApplicationContext(), "Enter Your OTP First", Toast.LENGTH_SHORT).show();
}
else{
mprogressbarofotpauth.setVisibility(View.VISIBLE);
String coderecieved = getIntent().getStringExtra("otp");
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(coderecieved, enteredotp);
signInWithPhoneAuthCredential(credential);
}
}
});
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
firebaseAuth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull @org.jetbrains.annotations.NotNull Task<AuthResult> task) {
if (task.isSuccessful()) {
mprogressbarofotpauth.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "Login Successful!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(otpAuthentication.this, setProfile.class);
startActivity(intent);
finish();
} else {
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
mprogressbarofotpauth.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "Login Failed!", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
这是我的gradle 依赖项
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation "androidx.browser:browser:1.3.0"
implementation 'com.google.firebase:firebase-auth:21.0.1'
implementation 'com.google.firebase:firebase-database:20.0.1'
implementation 'com.google.firebase:firebase-firestore:23.0.3'
implementation 'com.google.firebase:firebase-storage:20.0.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.intellij:annotations:+@jar'
implementation 'com.hbb20:ccp:2.5.1'
}
如果有人想要我在此处输入验证号码的 MainActivity,那就是
public class MainActivity extends AppCompatActivity {
EditText mgetphonenumber;
android.widget.Button msendotp;
CountryCodePicker mcountrycodepicker;
//extra m is added for for easily assigning XML ID to Java ID
String countrycode;
String phonenumber;
FirebaseAuth firebaseAuth;
ProgressBar mprogressbarofmain;
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
String codesent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mcountrycodepicker = findViewById(R.id.countrycodepicker);
msendotp = findViewById(R.id.sendotpbutton);
mgetphonenumber = findViewById(R.id.getphonenumber);
mprogressbarofmain = findViewById(R.id.progressbarofmain);
firebaseAuth = FirebaseAuth.getInstance();
countrycode = mcountrycodepicker.getSelectedCountryCodeWithPlus();
//below segment is for those who want to change their countries
mcountrycodepicker.setOnCountryChangeListener(new CountryCodePicker.OnCountryChangeListener() {
@Override
public void onCountrySelected() {
countrycode = mcountrycodepicker.getSelectedCountryCodeWithPlus();
}
});
//for sending otp
msendotp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String number;
number = mgetphonenumber.getText().toString();
//if the user doesn't enters the number
if(number.isEmpty()){
Toast.makeText(getApplicationContext(), "Please Enter Your Number", Toast.LENGTH_SHORT).show();
}
//since a number has 10 digits
else if(number.length()<10){
Toast.makeText(getApplicationContext(), "Please Enter a Valid Number", Toast.LENGTH_SHORT).show();
}
else{
mprogressbarofmain.setVisibility(View.VISIBLE);
//concatenates country code and the number added by the user
phonenumber = countrycode + number;
PhoneAuthOptions options = PhoneAuthOptions.newBuilder(firebaseAuth)
.setPhoneNumber(phonenumber)
.setTimeout(60L, TimeUnit.SECONDS)
.setActivity(MainActivity.this)
.setCallbacks(mCallbacks)
.build();
PhoneAuthProvider.verifyPhoneNumber(options);
}
}
});
//function to verify the number for otp
mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
//how to automatically fetch OTP(for future)
}
@Override
public void onVerificationFailed(@NonNull FirebaseException e) {
}
@Override
public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
//if the function runs successfully, the code is sent
Toast.makeText(getApplicationContext(), "OTP is Sent", Toast.LENGTH_SHORT).show();
mprogressbarofmain.setVisibility(View.INVISIBLE);
codesent = s;
Intent intent = new Intent(MainActivity.this, otpAuthentication.class);
intent.putExtra("otp", codesent);
startActivity(intent);
}
};
}
//for checking if the user exists(in order to open the chat screen)
@Override
protected void onStart() {
super.onStart();
if(FirebaseAuth.getInstance().getCurrentUser()!=null){
Intent intent = new Intent(MainActivity.this, chatscreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
}
解决方案
它看起来像一个与 Firebase 无关的问题。
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.ProgressBar.setVisibility(int)”
你确定你的进度条已经初始化了吗?也许 findViewById 没有做它需要做的事情?也许在布局上,进度条视图 id 不一样?
推荐阅读
- ios - 音频切割器原生 iOS 元素
- javascript - 组件内的人口数组
- asp.net - Asp Net Core URL Rewrite 不下载 CSS JS 文件
- python - 如何在 flask-jwt-extended 中获取当前用户(id)?
- r - 如何在不改变原点值的情况下使用 ggplot2 指定图表中第一个刻度的值?
- ios - 命令 PhaseScriptExecution 以非零退出代码 Xcode 11.3.1 失败
- linux - 是否需要为 TAILQ_EMPTY() 操作获取锁
- powerbi - 我想根据列的不同元素创建新表?
- reactjs - 没有状态对象的组件可以称为有状态组件吗
- docusignapi - Docusign API:更新信封文档时页面大小被锁定