造成原因:java.lang.IllegalStateException

时间:2019-01-06 15:00:45

标签: java android

01-06 20:22:35.822 17166 17166 E AndroidRuntime位于android.os.Handler.dispatchMessage(Handler.java:108) 01-06 20:22:35.822 17166 17166 E AndroidRuntime位于android.os.Looper.loop(Looper.java:166) 01-06 20:22:35.822 17166 17166 E AndroidRuntime位于android.app.ActivityThread.main(ActivityThread.java:7523) 01-06 20:22:35.822 17166 17166 E AndroidRuntime位于java.lang.reflect.Method.invoke(本机方法) 01-06 20:22:35.822 17166 17166 E位于com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:245)的AndroidRuntime 01-06 20:22:35.822 17166 17166 E位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)的AndroidRuntime 01-06 20:22:35.822 17166 17166 E AndroidRuntime造成原因:java.lang.IllegalStateException:在此过程com.mycompany.myapp2中未初始化默认FirebaseApp。确保首先调用FirebaseApp.initializeApp(Context)。 01-06 20:22:35.822 17166 17166 E位于com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common @@ 16.0.2:240)的AndroidRuntime 01-06 20:22:35.822 17166 17166 E位于com.google.firebase.auth.FirebaseAuth.getInstance的AndroidRuntime(未知来源:1) 01-06 20:22:35.822 17166 17166 E AndroidRuntime在com.mycompany.myapp2.VerifyPhoneActivity.onCreate(VerifyPhoneActivity.java:39) 01-06 20:22:35.822 17166 17166 E AndroidRuntime位于android.app.Activity.performCreate(Activity.java:7383) 01-06 20:22:35.822 17166 17166 E Android运行时位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218) 01-06 20:22:35.822 17166 17166 E AndroidRuntime位于android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3250) 01-06 20:22:35.822 17166 17166 E AndroidRuntime ...还有9个

公共类VerifyPhoneActivity扩展了活动{

private String verificationId;
private FirebaseAuth mAuth;
private ProgressBar progressBar;
private EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FirebaseApp.initializeApp(VerifyPhoneActivity.this);
    setContentView(R.layout.phone);

    mAuth = FirebaseAuth.getInstance();

    progressBar = findViewById(R.id.progressbar);
    editText = findViewById(R.id.editTextCode);

    String phonenumber = getIntent().getStringExtra("phonenumber");
    sendVerificationCode(phonenumber);

    findViewById(R.id.buttonSignIn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String code = editText.getText().toString().trim();

                if (code.isEmpty() || code.length() < 6) {

                    editText.setError("Enter code...");
                    editText.requestFocus();
                    return;
                }
                verifyCode(code);
            }
        });

}

private void verifyCode(String code) {
    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
    signInWithCredential(credential);
}

private void signInWithCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
        .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {

                    Intent intent = new Intent(VerifyPhoneActivity.this, ProfileActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

                    startActivity(intent);

                } else {
                    Toast.makeText(VerifyPhoneActivity.this, task.getException().getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
}

private void sendVerificationCode(String number) {
    progressBar.setVisibility(View.VISIBLE);
    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) {
            editText.setText(code);
            verifyCode(code);
        }
    }

    @Override
    public void onVerificationFailed(FirebaseException e) {
        Toast.makeText(VerifyPhoneActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
};

}

1 个答案:

答案 0 :(得分:0)

java.lang.IllegalStateException:在此过程com.mycompany.myapp2中未初始化默认FirebaseApp。确保调用FirebaseApp.initializeApp(Context)

FirebaseApp.initializeApp(this);

致电之前

FirebaseAuth.getInstance();