首页 > 解决方案 > 为什么当我在 Android 中强制删除暗模式时,我的登录 Activity 会弹出错误

问题描述

我只是写了一个简单的暗模式移除器代码然后突然我的应用程序崩溃了这在 OncreateMethod 中显示错误,我描述了绑定这是在将此代码放入启动器活动后发生的,当我评论说它将自动高效运行时,反之亦然我的安卓手机的暗模式弹出错误为什么会发生

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

这是我的登录活动:

public class LoginActivity extends AppCompatActivity {

ActivityLoginBinding binding;

SharedPreferences preferences;
private static final String LOGIN_CHECK = "MyLoginCheck";
private static final String KEY_MOBILE = "mobile";



private FirebaseAuth auth;
private String codeSent;
private String countrycode;

ProgressDialog sendingOtpProgressDialog;
ProgressDialog proceedProgressDialog;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityLoginBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    preferences = getSharedPreferences(LOGIN_CHECK, MODE_PRIVATE);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(ContextCompat.getColor(getApplicationContext(), R.color.blue_900));
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }

    initElements();

}

private void initElements() {

    auth = FirebaseAuth.getInstance();      // For Firebase Auth

//        For Country code picker
//        binding.countryCodePicker.registerCarrierNumberEditText(binding.getStartedMobileEd);
    countrycode = binding.countryCodePicker.getSelectedCountryCodeWithPlus();

//        For Sending Otp Progress Dialog
    sendingOtpProgressDialog = new ProgressDialog(LoginActivity.this);
    sendingOtpProgressDialog.setTitle("Sending OTP");
    sendingOtpProgressDialog.setMessage("please wait...");

//        For Authenticate user Progress Dialog
    proceedProgressDialog = new ProgressDialog(LoginActivity.this);
    proceedProgressDialog.setTitle("Verifying");
    proceedProgressDialog.setMessage("please wait...");

//        Default error is null
    binding.getStartedMobile.setError(null);
    binding.getStartedOTP.setError(null);
    binding.proceed.setEnabled(false);

//        For sending Otp to the user
    binding.sendOTP.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendingOtpProgressDialog.show();
            sendingOtpProgressDialog.setCanceledOnTouchOutside(false);
            binding.getStartedMobile.setEnabled(false);
            sendOTPonClick();
        }
    });

//        For Verifying otp and log In user
    binding.proceed.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            proceedOnClick();
        }
    });

}


//    All OTP methods
private void sendOTPonClick() {

    String mobileNumber = countrycode + binding.getStartedMobileEd.getText().toString();

    if (binding.getStartedMobileEd.getText().toString().isEmpty()) {
        binding.getStartedMobile.setError("Required");
        binding.getStartedMobile.requestFocus();
        sendingOtpProgressDialog.dismiss();
        return;
    } else if (binding.getStartedMobileEd.getText().toString().length() != 10) {
        binding.getStartedMobile.setError("Not Valid");
        binding.getStartedMobile.requestFocus();
        sendingOtpProgressDialog.dismiss();
        return;
    } else {
        PhoneAuthOptions authOptions = PhoneAuthOptions.newBuilder(auth)
                .setPhoneNumber(mobileNumber)
                .setTimeout(60L, TimeUnit.SECONDS)
                .setActivity(LoginActivity.this)
                .setCallbacks(mCallbacks)
                .build();
        PhoneAuthProvider.verifyPhoneNumber(authOptions);
    }

}

private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
    @Override
    public void onVerificationCompleted(@NonNull PhoneAuthCredential phoneAuthCredential) {
//            This method is no longer Required
    }

    @Override
    public void onVerificationFailed(@NonNull FirebaseException e) {

    }


    @Override
    public void onCodeSent(@NonNull String s, @NonNull PhoneAuthProvider.ForceResendingToken forceResendingToken) {
        super.onCodeSent(s, forceResendingToken);
        codeSent = s;
        sendingOtpProgressDialog.dismiss();
        binding.proceed.setEnabled(true);
        Toast.makeText(getApplicationContext(), "OTP is Sent", Toast.LENGTH_SHORT).show();
        binding.sendOTP.setVisibility(View.INVISIBLE);
        binding.countdownTimer.setVisibility(View.VISIBLE);

        new CountDownTimer(60000, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                binding.countdownTimer.setText("" + millisUntilFinished / 1000);
            }

            @Override
            public void onFinish() {
                binding.countdownTimer.setVisibility(View.INVISIBLE);
                binding.sendOTP.setText("Resend");
                binding.sendOTP.setVisibility(View.VISIBLE);
            }
        }.start();

    }

};


//    All Proceed Methods
private void proceedOnClick() {

    if (binding.getStartedMobileEd.getText().toString().isEmpty()) {
        binding.getStartedMobile.setError("Required*");
    } else if (binding.getStartedOTPEd.getText().toString().isEmpty()) {
        binding.getStartedOTP.setError("Required*");
    } else {
        proceedProgressDialog.show();
        proceedProgressDialog.setCanceledOnTouchOutside(false);
        String code = binding.getStartedOTPEd.getText().toString();
        PhoneAuthCredential credential = PhoneAuthProvider.getCredential(codeSent, code);
        signInWithPhoneAuthCredential(credential);
    }

}

private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {


    auth.signInWithCredential(credential).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
        @Override
        public void onComplete(@NonNull Task<AuthResult> task) {
            if (task.isSuccessful()) {
                proceedProgressDialog.dismiss();
                FirebaseUser user = auth.getCurrentUser();
                String currentUserMobile = user.getPhoneNumber();
//                    Toast.makeText(LoginActivity.this, currentUserMobile, Toast.LENGTH_LONG).show();

                CollectionReference reference = FirebaseFirestore.getInstance().collection("Users");
                Query query = reference.whereEqualTo("Mobile", currentUserMobile);
                query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (DocumentSnapshot snapshot : task.getResult()) {
                                String userCred = snapshot.getString("Mobile");
                                if (userCred.equals(currentUserMobile)) {

                                    SharedPreferences.Editor editor = preferences.edit();
                                    editor.putString(KEY_MOBILE, currentUserMobile);
                                    editor.commit();

                                    Intent intent = new Intent(LoginActivity.this, DashboardActivity.class);
                                    startActivity(intent);
                                    finishAffinity();

                                }
                            }
                        }
                        if (task.getResult().size() == 0) {
                            Toast.makeText(LoginActivity.this, "User Not Exist", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(LoginActivity.this, AdditionalDetailsActivity.class);
                            startActivity(intent);
                            finish();
                        }

                    }
                });
            } else {
                if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                    proceedProgressDialog.dismiss();
                    binding.getStartedOTP.setError("Invalid OTP");
                    Toast.makeText(LoginActivity.this, "Incorrect OTP entered", Toast.LENGTH_LONG).show();
                }
            }
        }
    });
}


//    To exit from the application
@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(false);
    builder.setMessage("Do you want to Exit?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //if user pressed "yes", then he is allowed to exit from application
            finish();
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //if user select "No", just cancel this dialog and continue with app
            dialog.cancel();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

}

这里的错误是:

2021-11-14 14:54:16.664 16604-16604/com.gamestockz E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gamestockz, PID: 16604
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gamestockz/com.gamestockz.Activities.LoginActivity}: android.view.InflateException: Binary XML file line #205: Binary XML file line #205: Error inflating class <unknown>
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:201)
    at android.app.ActivityThread.main(ActivityThread.java:6826)
    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)
 Caused by: android.view.InflateException: Binary XML file line #205: Binary XML file line #205: Error inflating class <unknown>
 Caused by: android.view.InflateException: Binary XML file line #205: Error inflating class <unknown>
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at android.view.LayoutInflater.createView(LayoutInflater.java:651)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:867)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:870)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:870)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:870)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:870)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
    at com.gamestockz.databinding.ActivityLoginBinding.inflate(ActivityLoginBinding.java:101)
    at com.gamestockz.databinding.ActivityLoginBinding.inflate(ActivityLoginBinding.java:95)
    at com.gamestockz.Activities.LoginActivity.onCreate(LoginActivity.java:62)  // here's the error link was shown in blue highlight
    at android.app.Activity.performCreate(Activity.java:7224)
    at android.app.Activity.performCreate(Activity.java:7213)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2926)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3081)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1831)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:201)
    at android.app.ActivityThread.main(ActivityThread.java:6826)
    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)
2021-11-14 14:54:16.665 16604-16604/com.gamestockz E/AndroidRuntime: Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f110090
    at android.content.res.Resources.getText(Resources.java:360)
    at android.content.res.MiuiResources.getText(MiuiResources.java:97)
    at android.content.res.Resources.getString(Resources.java:453)
    at android.view.View.<init>(View.java:5445)
    at android.widget.TextView.<init>(TextView.java:892)
    at android.widget.EditText.<init>(EditText.java:88)
    at android.widget.EditText.<init>(EditText.java:84)
    at androidx.appcompat.widget.AppCompatEditText.<init>(AppCompatEditText.java:93)
    at com.google.android.material.textfield.TextInputEditText.<init>(TextInputEditText.java:64)
    at com.google.android.material.textfield.TextInputEditText.<init>(TextInputEditText.java:59)
        ... 35 more

我找不到我做错了什么

标签: javaandroidfirebaseandroid-studioruntime-error

解决方案


推荐阅读