【问题标题】:Why TextInputEditText cannot be cast to TextInputLayout in Android Studio?为什么 TextInputEditText 不能在 Android Studio 中转换为 TextInputLayout?
【发布时间】:2022-01-16 09:09:42
【问题描述】:

所以基本上我遇到了转换错误,我尝试将 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();

    }
}

其对应的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>



所以基本上当我运行这段代码时,我得到了这个错误 java.lang.ClassCastException: com.google.android.material.textfield.TextInputLayout cannot be cast to com.google.android.material.textfield.TextInputEditText

任何帮助将不胜感激

【问题讨论】:

    标签: java android firebase android-studio


    【解决方案1】:

    那是因为它们是不同类型的不同项目。您不能将 Layout 类型转换为 EditText 类型。如果您想要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>
    

    那你就可以了

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

    【讨论】:

      猜你喜欢
      • 2018-10-13
      • 2020-09-28
      • 2018-03-15
      • 1970-01-01
      • 2017-04-18
      • 2019-12-02
      • 1970-01-01
      • 2012-10-17
      • 2016-09-08
      相关资源
      最近更新 更多