简体   繁体   中英

Why TextInputEditText cannot be cast to TextInputLayout in Android Studio?

So basically I'm getting a casting error, I tried to bind the ids with the same kind of widgets and class names.In the java file I already declared the variables with TextInputEditText instead of TextInputLayout and casted with TextInputEditText but It doesn't seem to work.Anyways, This is my activity code:

public class Match extends AppCompatActivity {
    TextInputEditText nomstade,prix,date,nbreplace;
    Button submitbutton;
    DatabaseReference matchDBRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.creatematch);
        nomstade =  (TextInputEditText) findViewById(R.id.nomstade);
        prix =(TextInputEditText)findViewById(R.id.prix);
        submitbutton=findViewById(R.id.submitbutton);
        matchDBRef= FirebaseDatabase.getInstance().getReference().child("Matchs");
        submitbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                insertMatchData();

            }
        });

    }
    private void insertMatchData(){
        String Stade= nomstade.getText().toString();
        String Prix= prix.getText().toString();
      

        Matchs matchs= new Matchs(Stade,Prix);
matchDBRef.push().setValue(matchs);
        Toast.makeText(Match.this,"Match created",Toast.LENGTH_SHORT).show();

    }
}

Its corresponding XML code is:

 <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/nomstade"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="32dp"
        android:hint="Nom du stade"
        app:boxBackgroundMode="outline"
        app:boxCornerRadiusTopEnd="16dp"
        app:hintTextColor="#56E07B"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        tools:ignore="DuplicateIds">

        <com.google.android.material.textfield.TextInputEditText

            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/nbreplace"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="32dp"

        android:layout_marginTop="40dp"
        android:layout_marginEnd="32dp"
        android:hint="Nombre de place"
        app:boxBackgroundMode="outline"
        app:boxCornerRadiusTopEnd="16dp"
        app:hintTextColor="#56E07B"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/nomstade">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </com.google.android.material.textfield.TextInputLayout>



So basically when I run this code I get this error java.lang.ClassCastException: com.google.android.material.textfield.TextInputLayout cannot be cast to com.google.android.material.textfield.TextInputEditText

Any help would be appreciated

That is because they are different items with different types. You can't cast the Layout type to the EditText type. If you want the TextInputEditText you should assign an ID to the TextInputEditText and find that instead of the layout parent ID.

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/nbreplace"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="32dp"
    android:layout_marginTop="40dp"
    android:layout_marginEnd="32dp"
    android:hint="Nombre de place"
    app:boxBackgroundMode="outline"
    app:boxCornerRadiusTopEnd="16dp"
    app:hintTextColor="#56E07B"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/nomstade">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/nbreplace_text
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>

Then you could do

nbreplace = (TextInputEditText) findViewById(R.id.nbreplace_text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM