我的应用程序在高于21的API级别上正常工作,但它在较低的API级别上不断崩溃

时间:2018-01-17 18:23:51

标签: java android

compileSDKVersion是26

minSDKVersion是15

logcat向我显示此错误

Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class Button

并且错误指向这一主要活动

setContentView(R.layout.activity_main);

我的主要活动是

公共类MainActivity扩展了AppCompatActivity {

Button btnSignIn,btnRegister;
RelativeLayout rootLayout;


FirebaseAuth auth;
FirebaseDatabase db;
DatabaseReference users;

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                                                        .setDefaultFontPath("fonts/Arkhip_font.ttf")
                                                        .setFontAttrId(R.attr.fontPath)
                                                        .build());
    setContentView(R.layout.activity_main);

    //initialization of firebase

    auth = FirebaseAuth.getInstance();
    db=FirebaseDatabase.getInstance();
    users=db.getReference("Users");

    btnRegister =(Button)findViewById(R.id.btnRegister);
    btnSignIn=(Button)findViewById(R.id.btnSignIn);
    rootLayout= (RelativeLayout) findViewById(R.id.rootLayoutMax);

    //Event
    btnRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showRegisterDialog();
        }
    });

    btnSignIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showLoginDialog();
        }
    });

}

private void showLoginDialog() {
    final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setTitle("SIGN IN ");
    dialog.setMessage("Please use email to sign in");

    LayoutInflater inflater = LayoutInflater.from(this);
    View login_layout = inflater.inflate(R.layout.layout_login,null);

    final MaterialEditText edtEmail = login_layout.findViewById(R.id.edtEmail);
    final MaterialEditText edtPassword = login_layout.findViewById(R.id.edtPassword);

    dialog.setView(login_layout);

    //Setting Button

    dialog.setPositiveButton("SIGN IN", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {

                    dialogInterface.dismiss();

                    //Check validation
                    if (TextUtils.isEmpty(edtEmail.getText().toString())) {
                        Snackbar.make(rootLayout, "Please enter email address", Snackbar.LENGTH_LONG).show();
                        return;

                    }


                    if (TextUtils.isEmpty(edtPassword.getText().toString())) {
                        Snackbar.make(rootLayout, "Please enter password", Snackbar.LENGTH_LONG).show();
                        return;

                    }
                    if (edtPassword.getText().toString().length() < 6) {
                        Snackbar.make(rootLayout, "Password too short", Snackbar.LENGTH_LONG).show();
                        return;

                    }

                    //Login
                    auth.signInWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
                            .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                                @Override
                                public void onSuccess(AuthResult authResult) {
                                    startActivity(new Intent(MainActivity.this,Welcome.class));
                                    finish();

                                }
                            }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_SHORT)
                                    .show();

                        }
                    });
                }
            });
    dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });


    dialog.show();
}

private void showRegisterDialog() {
    final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
    dialog.setTitle("REGISTER ");
    dialog.setMessage("Please use email to register");

    LayoutInflater inflater = LayoutInflater.from(this);
    View register_layout = inflater.inflate(R.layout.layout_register,null);

    final MaterialEditText edtEmail = register_layout.findViewById(R.id.edtEmail);
    final MaterialEditText edtPassword = register_layout.findViewById(R.id.edtPassword);
    final MaterialEditText edtName = register_layout.findViewById(R.id.edtName);
    final MaterialEditText edtPhone = register_layout.findViewById(R.id.edtPhone);

    dialog.setView(register_layout);

    //Setting Button

    dialog.setPositiveButton("REGISTER", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            dialogInterface.dismiss();

            //Check validation
            if(TextUtils.isEmpty(edtEmail.getText().toString())){
                Snackbar.make(rootLayout,"Please enter email address",Snackbar.LENGTH_LONG).show();
                return;

            }

            if(TextUtils.isEmpty(edtPhone.getText().toString())){
                Snackbar.make(rootLayout,"Please enter phone number",Snackbar.LENGTH_LONG).show();
                return;

            }
            if(TextUtils.isEmpty(edtPassword.getText().toString())){
                Snackbar.make(rootLayout,"Please enter password",Snackbar.LENGTH_LONG).show();
                return;

            }
            if(edtPassword.getText().toString().length() < 6){
                Snackbar.make(rootLayout,"Password too short",Snackbar.LENGTH_LONG).show();
                return;

            }

            //Register new user
            auth.createUserWithEmailAndPassword(edtEmail.getText().toString(),edtPassword.getText().toString())
                    .addOnSuccessListener(new OnSuccessListener<AuthResult>() {
                        @Override
                        public void onSuccess(AuthResult authResult) {
                        //That will save user to db
                            User user = new User();
                            user.setEmail(edtEmail.getText().toString());
                            user.setPassword(edtPassword.getText().toString());
                            user.setName(edtName.getText().toString());
                            user.setPhone(edtPhone.getText().toString());


                            //It will use email to key
                            users.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                                    .setValue(user)
                            .addOnSuccessListener(new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void aVoid) {
                                    Snackbar.make(rootLayout,"Register success fully",Snackbar.LENGTH_LONG).show();

                                }
                            })

                            .addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.LENGTH_LONG).show();

                                }
                            });



                        }
                    })

            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {

    Snackbar.make(rootLayout,"Failed"+e.getMessage(),Snackbar.
    LENGTH_LONG).show();

                }
            });


        }
    });

    dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
        }
    });

    dialog.show();

}

}

和按钮backgroud xml是

<?xml version="1.0" encoding="utf-8"?>


<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="@color/rippleEffectColor"
tools:targetApi="lollipop"

>

<item>

    <shape android:shape="rectangle">
        <solid android:color="@android:color/white"></solid>
        <stroke android:color="@android:color/white" android:width="2dp">
</stroke>
        <corners android:radius="2dp"/>

    </shape>

 </item>



</ripple>

1 个答案:

答案 0 :(得分:0)

在API 21中添加了Ripple,因此不能在下面使用它。 查看此库,以便在您的视图中为以下API 21添加涟漪效果 https://github.com/balysv/material-ripple