Firebase电话号码认证出现空引用问题。

huangapple go评论71阅读模式
英文:

firebase phone number authentication getting null reference

问题

我正试图在我的应用程序中创建一个电话号码记录部分,以便用户可以使用电话号码登录。我试图在物理设备上运行我的应用程序,当我尝试使用电话号码登录时,收到一个错误,显示空引用。我已经在互联网上搜索了解决方案,但没有得到任何适当的解决方案来解决这个错误。我已经在 Firebase 中允许了电话验证,但仍然出现错误。我在我的活动中使用了国家选择器来获取国家代码,它也正常工作。

错误发生在:

PhoneAuthProvider.getInstance().verifyPhoneNumber(
    phonestring,
    60,
    TimeUnit.SECONDS,
    Phoneactivity.this,
    mCallbacks
);

Phoneactivity.java:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_phoneactivity);

    mAuth = FirebaseAuth.getInstance();

    initalization();
    phonenumbermethod();
    emailloginmethod();
}

// 其他方法的代码...

错误信息:空引用

java.lang.NullPointerException: 空引用
at com.google.android.gms.common.internal.Preconditions.checkNotNull(Unknown Source:2)
at com.google.firebase.auth.PhoneAuthProvider.verifyPhoneNumber(com.google.firebase:firebase-auth@@19.2.0:9)
at com.nanb.Alpha.Phoneactivity.verificationcodesend(Phoneactivity.java:109)
at com.nanb.Alpha.Phoneactivity.access$300(Phoneactivity.java:28)
at com.nanb.Alpha.Phoneactivity$2.onClick(Phoneactivity.java:72)
at android.view.View.performClick(View.java:6608)
at android.view.View.performClickInternal(View.java:6585)
at android.view.View.access$3100(View.java:785)
at android.view.View$PerformClick.run(View.java:25921)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6864)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

更新:

2020-03-15 21:03:00.382 30384-30384/com.nanb.Alpha I/phoneactivity: +919771553694
英文:

I am trying to create a phone number logging section in my application so that the user can log in using the phone number. I am trying to run my application on the physical device when I try to login using the phone number and received an error says the null reference. I have searched for the solution all over the internet but didn't get any proper solution to remove this error. I have allowed the phone authentication in firebase still, I am getting the error. I have used the country picker in my activity to get the country code and it works file.

Error occurs in

     PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phonestring,
                60,
                TimeUnit.SECONDS,
                Phoneactivity.this,
                mCallbacks
        );

Phoneactivity.java

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_phoneactivity);

    mAuth = FirebaseAuth.getInstance();

    initalization();
    phonenumbermethod();
    emailloginmethod();
}

private void emailloginmethod() {
    emaillogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(),Loginactivity.class);
            startActivity(intent);
        }
    });
}

private void phonenumbermethod() {
    if(REQUEST.equals("phone")){
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                REQUEST = "OTP";
                String phonenumberstring = phone.getText().toString();
                String countrycode = ccp.getSelectedCountryCodeWithPlus();
                phonestring = countrycode + phonenumberstring;
                //Toast.makeText(getApplicationContext(),phonestring,Toast.LENGTH_SHORT).show();
                verificationcodesend();
            }
        });
    }else{
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                REQUEST = "phone";
                otpstring = otp.getText().toString();
                otpmethod();
            }
        });
    }
}

private void otpmethod() {
    if (TextUtils.isEmpty(otpstring)){
        Toast.makeText(getApplicationContext(),"Please enter the verification code", Toast.LENGTH_SHORT).show();
    }else{
        loadingBar.setTitle("Verification code");
        loadingBar.setMessage("Please wait...");
        loadingBar.setCanceledOnTouchOutside(false);
        loadingBar.show();
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(mVerificationId, otpstring);
        signInWithPhoneAuthCredential(credential);
    }
}

private void verificationcodesend() {
    if(TextUtils.isEmpty(phonestring)){
        Toast.makeText(getApplicationContext(),"Please enter phone number",Toast.LENGTH_SHORT).show();
    }else{
        loadingBar.setTitle("Phone verification");
        loadingBar.setMessage("Please wait till we verify your account");
        loadingBar.setCanceledOnTouchOutside(false);
        loadingBar.show();
        Log.i("phoneactivity",phonestring);
        PhoneAuthProvider.getInstance().verifyPhoneNumber(
                phonestring,
                60,
                TimeUnit.SECONDS,
                Phoneactivity.this,
                mCallbacks
        );
    }
    mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
        @Override
        public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
            signInWithPhoneAuthCredential(phoneAuthCredential);
        }

        @Override
        public void onVerificationFailed(@NonNull FirebaseException e) {
            loadingBar.dismiss();
            Toast.makeText(getApplicationContext(),"Please enter the correct phone number", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onCodeSent(@NonNull String verificationId,
                               @NonNull PhoneAuthProvider.ForceResendingToken token) {
            loadingBar.dismiss();

            mVerificationId = verificationId;
            mResendToken = token;
            Toast.makeText(getApplicationContext(),"Verification code has been send", Toast.LENGTH_SHORT).show();
            otpnumber.setVisibility(View.VISIBLE);
            phonenumber.setVisibility(View.GONE);
        }
    };
}
private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        loadingBar.dismiss();
                        sendusertomainActivity();
                        Toast.makeText(getApplicationContext(),"welcome",Toast.LENGTH_SHORT).show();
                    } else {
                        String msg = task.getException().toString();
                        Toast.makeText(getApplicationContext(),"Error: "+ msg, Toast.LENGTH_SHORT).show();
                    }
                }
            });
}

private void sendusertomainActivity() {
    Intent intent = new Intent(getApplicationContext(),HomeActivity.class);
    startActivity(intent);
}

error: null reference

      java.lang.NullPointerException: null reference
    at com.google.android.gms.common.internal.Preconditions.checkNotNull(Unknown Source:2)
    at com.google.firebase.auth.PhoneAuthProvider.verifyPhoneNumber(com.google.firebase:firebase-auth@@19.2.0:9)
    at com.nanb.Alpha.Phoneactivity.verificationcodesend(Phoneactivity.java:109)
    at com.nanb.Alpha.Phoneactivity.access$300(Phoneactivity.java:28)
    at com.nanb.Alpha.Phoneactivity$2.onClick(Phoneactivity.java:72)
    at android.view.View.performClick(View.java:6608)
    at android.view.View.performClickInternal(View.java:6585)
    at android.view.View.access$3100(View.java:785)
    at android.view.View$PerformClick.run(View.java:25921)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:201)
    at android.app.ActivityThread.main(ActivityThread.java:6864)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

update

  2020-03-15 21:03:00.382 30384-30384/com.nanb.Alpha I/phoneactivity: +919771553694

答案1

得分: 1

你在调用verifyPhoneNumber之前没有初始化mCallback,这就是空指针检查报错的原因。

要修复这个问题,将mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {...}这部分代码移到调用verifyPhoneNumber之前。

英文:

You're not initializing mCallback before passing it into verifyPhoneNumber, which is what the null check is complaining about.

To fix it, move the mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {... before the call to verifyPhoneNumber.

huangapple
  • 本文由 发表于 2020年3月15日 21:50:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/60693601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定