开发者社区> 问答> 正文

Firebase电话号码身份验证获取空引用

我试图在我的应用程序中创建电话号码记录部分,以便用户可以使用电话号码登录。当我尝试使用电话号码登录并收到一条错误消息时,我试图在物理设备上运行我的应用程序,并说空引用。我已经在整个互联网上搜索了解决方案,但没有任何适当的解决方案来消除此错误。我仍然在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();
}

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);
}

错误:空引用

 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)

更新

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

问题来源:Stack Overflow

展开
收起
montos 2020-03-24 14:02:38 736 0
1 条回答
写回答
取消 提交回答
  • mCallback在将其传递到之前,您无需进行初始化verifyPhoneNumber,这是null检查所抱怨的。

    要解决此问题,请将mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {...调用之前的移至verifyPhoneNumber。

    回答来源:Stack Overflow

    2020-03-24 14:02:53
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载