android NullPointerException 上的 SMS 身份验证
Posted
技术标签:
【中文标题】android NullPointerException 上的 SMS 身份验证【英文标题】:SMS authentication on android NullPointerException 【发布时间】:2019-04-21 13:10:29 【问题描述】:我收到了这个错误
更新错误:
E/androidRuntime: FATAL EXCEPTION: main
Process: xxx.com.xxx, PID: 4963
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.tasks.Task
com.google.android.gms.common.api.GoogleApi.zzb(com.google.android.gms.common.api.internal.zzdf)'
on a null object reference
at com.google.android.gms.internal.zzdtp.zzb(Unknown Source)
at com.google.android.gms.internal.zzdtw.zza(Unknown Source)
at com.google.firebase.auth.FirebaseAuth.zza(Unknown Source)
at com.google.firebase.auth.PhoneAuthProvider.zza(Unknown Source)
at com.google.firebase.auth.PhoneAuthProvider.verifyPhoneNumber(Unknown
Source)
at xxx.com.xxx.PhoneNumberActivity.onClick(PhoneNumberActivity.java:110)
at android.view.View.performClick(View.java:5265)
at android.view.View$PerformClick.run(View.java:21534)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5728)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
当我尝试运行这段代码时
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks); // OnVerificationStateChangedCallbacks
在应用程序 build.gradle 中
implementation 'com.google.firebase:firebase-database:11.6.0'
implementation 'com.google.firebase:firebase-auth:11.6.0'
为什么说它是空的?什么是空?我在 android 设备 Android 6.0 版上运行它。是因为手机上没有sim卡吗?我已经在控制台中启用了电话身份验证,并且它在 ios 上运行。
更新:
更新firebase-core和firebasse-auth版本后,应用不再崩溃。
implementation 'com.google.firebase:firebase-core:16.0.5'
implementation 'com.google.firebase:firebase-auth:16.0.5'
发现它是由我正在测试的手机的 8.3.01 (2385995-434) 版 Google Play 服务引起的。任何解决方案使其适用于 8.3.01?
错误:
" W/GooglePlayServicesUtil:Google Play 服务已过期。 需要 12451000 但找到 8301434"
问题:
如何控制最低 Google Play 服务版本?
【问题讨论】:
请拒绝投票的原因 是因为手机上没有 SIM 卡吗? -> 没有。我在没有 SIM 的情况下使用它,它正在工作。以及你得到什么变量null?
@RumitPatel 我已经更新了调用 PhoneAuthProvider.getInstance().verifyPhoneNumber 时的完整错误代码。我想知道应用程序崩溃的根本原因是什么。
将com.google.gms:google-services
、com.google.firebase:firebase-core
和com.google.firebase:firebase-auth
更新到最新版本并尝试。
我正在使用这些:classpath 'com.google.gms:google-services:4.1.0' implementation 'com.google.firebase:firebase-database:11.6.0' implementation 'com. google.firebase:firebase-auth:11.6.0' 不兼容吗?是否有显示哪个版本的 com.google.gms:google-services 与 com.google.firebase: 兼容的列表?比较混乱
【参考方案1】:
你可以看看这个工作代码。
build.gradle(应用级)
dependencies
implementation 'com.google.firebase:firebase-core:16.0.5'
implementation 'com.google.firebase:firebase-auth:16.0.5'
build.gradle(项目级)
buildscript
dependencies
classpath 'com.google.gms:google-services:4.1.0'
活动:
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
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.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.FirebaseTooManyRequestsException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.TimeUnit;
public class FCMsmsTest extends AppCompatActivity
private EditText editText;
private String mVerificationId = "";
PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks()
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential)
Toast.makeText(FCMsmsTest.this, "onVerificationCompleted " + phoneAuthCredential.toString(), Toast.LENGTH_SHORT).show();
signInWithPhoneAuthCredential(phoneAuthCredential);
@Override
public void onVerificationFailed(FirebaseException e)
Toast.makeText(FCMsmsTest.this, "onVerificationFailed " + e.toString(), Toast.LENGTH_SHORT).show();
if (e instanceof FirebaseAuthInvalidCredentialsException)
Toast.makeText(FCMsmsTest.this, "Invalid Request " + e.toString(), Toast.LENGTH_SHORT).show();
else if (e instanceof FirebaseTooManyRequestsException)
Toast.makeText(FCMsmsTest.this, "The SMS quota for the project has been exceeded " + e.toString(), Toast.LENGTH_SHORT).show();
@Override
public void onCodeSent(String verificationId,
PhoneAuthProvider.ForceResendingToken token)
Toast.makeText(FCMsmsTest.this, "onCodeSent " + verificationId, Toast.LENGTH_SHORT).show();
editText.setText("");
mVerificationId = verificationId;
PhoneAuthProvider.ForceResendingToken mResendToken = token;
showDialog();
;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fcm_sms_test);
editText = findViewById(R.id.edit_text);
findViewById(R.id.btn_send_sms).setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
if (editText.getText().toString().trim().length() > 9)
sendSMS("+91" + editText.getText().toString().trim());
else
Toast.makeText(FCMsmsTest.this, "Enter Valid PhoneNumber", Toast.LENGTH_SHORT).show();
);
private void sendSMS(String phoneNumber)
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks);
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential)
FirebaseAuth mAuth = FirebaseAuth.getInstance();
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>()
@Override
public void onComplete(@NonNull Task<AuthResult> task)
if (task.isSuccessful())
Toast.makeText(FCMsmsTest.this, "signInWithCredential:success", Toast.LENGTH_SHORT).show();
FirebaseUser user = task.getResult().getUser();
else
Toast.makeText(FCMsmsTest.this, "signInWithCredential:failure", Toast.LENGTH_SHORT).show();
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException)
Toast.makeText(FCMsmsTest.this, "The verification code entered was invalid", Toast.LENGTH_SHORT).show();
);
private void showDialog()
AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText edittextCode = new EditText(FCMsmsTest.this);
alert.setMessage("Enter Your Message");
alert.setTitle("Enter Your Title");
alert.setView(edittextCode);
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int whichButton)
String YouEditTextValueString = edittextCode.getText().toString();
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, YouEditTextValueString);
signInWithPhoneAuthCredential(credential);
);
alert.show();
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:gravity="center"
android:orientation="vertical"
android:padding="50dp">
<TableRow
android:layout_
android:layout_
android:gravity="center">
<TextView
android:layout_
android:layout_
android:text="+91" />
<EditText
android:id="@+id/edit_text"
android:layout_
android:layout_
android:inputType="phone" />
</TableRow>
<Button
android:id="@+id/btn_send_sms"
android:layout_
android:layout_
android:layout_marginTop="50dp"
android:text="Send OTP" />
</LinearLayout>
相应地更新您的项目。
【讨论】:
@user1872384 你检查了吗? Rumit Patel,我已经尝试升级版本,但仍然没有收到短信 我发现我正在使用的手机的版本 8.3.01 (2385995-434) Google Play 服务可能是根本原因,是否有任何文件说明它必须是高于 9.0.0 版?根据这个答案***.com/questions/43422021/… 但是在错误日志中它指出我需要版本“12.4.51.000”而不是“8.3.01.434” 您可以查看this question.以上是关于android NullPointerException 上的 SMS 身份验证的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android 中处理 NullPointerException?
<WebLogicServer; <BEA-000386; <Server subsystem failed. Reason: java.lang.NullPointerExcept
Maven打包时出现“Show Console View”错误弹出框,错误详情为“An internal error has occurred. java.lang.NullPointerExcept
Maven打包时出现“Show Console View”错误弹出框,错误详情为“An internal error has occurred. java.lang.NullPointerExcept