com.rengwuxian.materialedittext.MaterialEditText无法转换为android.view.ViewGroup

时间:2019-02-06 14:20:36

标签: java android

由于此错误,我的应用程序始终崩溃。它说:

com.rengwuxian.materialedittext.MaterialEditText cannot be cast to android.view.ViewGroup.

我是一名初学者,正在YouTube上寻找一些代码,但我不知道。

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="243dp"
    android:layout_height="166dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="60dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/logo" />

<TextView
    android:id="@+id/textView"
    android:layout_width="102dp"
    android:layout_height="24dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="44dp"
    android:layout_marginEnd="8dp"
    android:text="USERNAME"
    android:textSize="18sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.259"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/imageView2" />

<com.rengwuxian.materialedittext.MaterialEditText
    android:id="@+id/usernameTIL"
    android:layout_width="251dp"
    android:layout_height="46dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="12dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.527"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView">

    <com.rengwuxian.materialedittext.MaterialEditText
        android:layout_width="268dp"
        android:layout_height="36dp"
        android:hint="username" />
</com.rengwuxian.materialedittext.MaterialEditText>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="PASSWORD"
    android:textSize="18sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.255"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/usernameTIL"
    app:layout_constraintVertical_bias="0.018" />

<com.rengwuxian.materialedittext.MaterialEditText
    android:id="@+id/passwordTIL2"
    android:layout_width="253dp"
    android:layout_height="47dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="12dp"
    android:layout_marginEnd="8dp"
    android:ems="10"
    android:hint="password"
    android:inputType="textPassword"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.535"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView2" />

<CheckBox
    android:id="@+id/rememberCB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="68dp"
    android:text="Remember Me?"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/passwordTIL2" />

<Button
    android:id="@+id/signInBTN"
    android:layout_width="145dp"
    android:layout_height="49dp"
    android:layout_marginTop="28dp"
    android:background="@color/signInBTN"
    android:text="SIGN IN"
    android:textColor="@android:color/white"
    android:textSize="18sp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/rememberCB" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="Dont't have an Account?"
    android:textColor="@android:color/black"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.323"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/signInBTN" />

我的Java:

public class sign_in extends AppCompatActivity {
EditText usernameTIL, passwordTIL;
Button signInBTN;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in);

    usernameTIL = (MaterialEditText) findViewById(R.id.usernameTIL);
    passwordTIL = (MaterialEditText) findViewById(R.id.passwordTIL2);
    signInBTN = (Button) findViewById(R.id.signInBTN);

    //Init FireBase
    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference table_user = database.getReference("User");

    signInBTN.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final ProgressDialog mDialog = new Progress codeDialog(sign_in.this);
            mDialog.setMessage("Please wait...");
            mDialog.show();

            table_user.addValueEventListener(new ValueEventListener() {

                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    //check if user doesn't have an account
                    if (dataSnapshot.child(usernameTIL.getText().toString()).exists()) {
                        //get user information
                        mDialog.dismiss();
                        User user = dataSnapshot.child(usernameTIL.getText().toString()).getValue(User.class);
                        if (user.getPassword().equals(passwordTIL.getText().toString())) {
                            Intent homeIntent = new Intent(sign_in.this, HomeScreen.class);
                            Common.curentUser = user;
                            startActivity(homeIntent);
                            finish();
                        } else {
                            Toast.makeText(sign_in.this, "Sign In Failed !!", Toast.LENGTH_SHORT).show();
                        }

                    }

                    else
                    {
                        Toast.makeText(sign_in.this, "User not found !!", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

        }
    });
}

}

2 个答案:

答案 0 :(得分:0)

MaterialEditTextEditText

public class MaterialEditText extends AppCompatEditText {

它不能包含任何小部件。在您的布局中,您有

<com.rengwuxian.materialedittext.MaterialEditText
    android:id="@+id/usernameTIL"
    android:layout_width="251dp"
    android:layout_height="46dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="12dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.527"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView">

    <com.rengwuxian.materialedittext.MaterialEditText
        android:layout_width="268dp"
        android:layout_height="36dp"
        android:hint="username" />
</com.rengwuxian.materialedittext.MaterialEditText>

一个MaterialEditText包含另一个MaterialEditText,这导致您的应用因ClassCastException崩溃

答案 1 :(得分:0)

根据该库的示例代码,您必须像这样将MaterialEditText强制转换为EditText

usernameTIL = (EditText) findViewById(R.id.usernameTIL);
passwordTIL = (EditText) findViewById(R.id.passwordTIL2);