简体   繁体   English

为什么 TextInputEditText 在 Android Studio 中无法转换为 TextInputLayout?

[英]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:所以基本上我遇到了一个转换错误,我试图用相同类型的小部件和 class 名称绑定 ID。在 java 文件中,我已经用 TextInputEditText 而不是 TextInputLayout 声明了变量,并用 TextInputEditText 进行了转换,但它似乎没有工作。无论如何,这是我的活动代码:

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:其对应的XML代码为:

 <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 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.您不能将 Layout 类型转换为 EditText 类型。 If you want the TextInputEditText you should assign an ID to the TextInputEditText and find that instead of the layout parent ID.如果您想要TextInputEditText ,您应该为TextInputEditText分配一个 ID,并找到它而不是布局父 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android 应用程序中的“TextInputEditText 无法转换为 TextInputLayout” - “TextInputEditText cannot be cast to TextInputLayout” in Android app Android 材质 TextInputLayout 和 TextInputEditText - Android Material TextInputLayout and TextInputEditText Android TextInputEditText/TextInputLayout 切断错误信息 - Android TextInputEditText/TextInputLayout cuts off error message android.view.InflateException:二进制 XML 文件第 27 行:android.support.design.widget.TextInputEditText 不能转换为 android.view.ViewGroup - android.view.InflateException: Binary XML file line #27: android.support.design.widget.TextInputEditText cannot be cast to android.view.ViewGroup 如何从 TextInputLayout 或 TextInputEditText 中删除下划线 - How can remove underline from TextInputLayout or TextInputEditText java.util.HashMap 不能投(android studio, firebase,java) - java.util.HashMap cannot be cast (android studio, firebase,java) Android Studio错误-类型不可转换,无法转换片段 - Android Studio error - Inconvertible types, Cannot cast Fragment Android Studio - 不可转换的类型无法将视图转换为我的活动 - Android Studio - Inconvertible types cannot cast View to my activity 为什么TextInputEditText会产生此错误? - Why is TextInputEditText producing this error? 为什么无法在android中的Widget按钮中投射imageview - why imageview cannot be cast in android widget button in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM