Я застрял в этой ошибке, говоря NullException, но я уже инициализировал свои объекты, но она все еще показывает нулевое исключение.Как ее решить, я снова и снова просматриваю программу и устанавливаю точки останова, но безрезультатно
Код:
package com.rishavgmail.coder.carescountrysactionresourcesexigencyservices;
import android.arch.core.executor.TaskExecutor;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.TaskExecutors;
import com.google.firebase.FirebaseException;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class VerifyPhone extends AppCompatActivity {
private CoordinatorLayout coordinatorLayoutVerifyPhone;
private TextInputLayout textInputLayoutOTP;
private Button btnVerifyOTP;
private String verificationID;
private FirebaseAuth mAuth;
private ProgressBar progressBarVerifyPhone;
private String phoneNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAuth = FirebaseAuth.getInstance();
String phoneNumber = getIntent().getStringExtra("phoneNumber");
Initialization();
sendVerificationCode(phoneNumber);
btnVerifyOTP = findViewById(R.id.btnVerifyOTP);
btnVerifyOTP.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String code = textInputLayoutOTP.getEditText().getText().toString().trim();
if (code.isEmpty())
{
textInputLayoutOTP.setError("Enter OTP");
return;
}
else if (code.length()<6)
{
textInputLayoutOTP.setError("Enter OTP");
return;
}
progressBarVerifyPhone.setVisibility(View.VISIBLE);
verifyCode(code);
}
});
}
private void verifyCode(String code)
{
PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationID,code);
ProceedToRegister(credential);
}
private void ProceedToRegister(PhoneAuthCredential credential)
{
mAuth.signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful())
{
Intent intent=new Intent(VerifyPhone.this,AuthorityRegistration.class);
String AuthPhone = textInputLayoutOTP.getEditText().getText().toString().trim();
intent.putExtra("AuthPhone",AuthPhone);
startActivity(intent);
}
else
{
Snackbar snackbar = Snackbar.make(coordinatorLayoutVerifyPhone,"Something went wrong !!",Snackbar.LENGTH_SHORT);
snackbar.show();
}
}
});
}
private void Initialization()
{
progressBarVerifyPhone = findViewById(R.id.progressBarVerifyPhone);
textInputLayoutOTP = findViewById(R.id.textInputLayoutVerifyOTP);
coordinatorLayoutVerifyPhone = findViewById(R.id.coordinatorLayoutVerifyPhone);
}
private void sendVerificationCode(String number)
{
PhoneAuthProvider.getInstance().verifyPhoneNumber(number,60,TimeUnit.SECONDS, TaskExecutors.MAIN_THREAD,mCallBack);
}
private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallBack =
new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
@Override
public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
super.onCodeSent(s, forceResendingToken);
verificationID = s;
}
@Override
public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
String code = phoneAuthCredential.getSmsCode();
if (code!=null)
{
progressBarVerifyPhone.setVisibility(View.VISIBLE);
verifyCode(code);
}
}
@Override
public void onVerificationFailed(FirebaseException e) {
Snackbar snackbar = Snackbar.make(coordinatorLayoutVerifyPhone,"Verification Failed !!",Snackbar.LENGTH_SHORT);
snackbar.show();
}
};
}
Ошибка:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.rishavgmail.coder.carescountrysactionresourcesexigencyservices.VerifyPhone.onCreate(VerifyPhone.java:44)
ОтправляетсяOTP из Firebase, но не запускает операцию VerifyOTP для проверки