com.google.android.material.textview.MaterialTextView不能强制转换为com.google.android.material.textfield.TextInputEditText

iyfjxgzm  于 2023-05-27  发布在  Android
关注(0)|答案(1)|浏览(269)

我试图在我的片段中使用导航组件,但我在使用绑定扩展布局时遇到了问题。看,这是我的片段xml,这是fragment.kt代码:
这就是错误:
com.google.android.material.textview.MaterialTextView不能强制转换为com.google.android.material.textfield.TextInputEditText

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background_white_color"
    tools:context=".home.presentation.view.EditNoteScreen">

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/mine_notes_tittle_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="58dp"
        android:layout_marginTop="18dp"
        android:fontFamily="@font/roboto"
        android:text="@string/new_note"
        android:textColor="@color/grey_text"
        android:textSize="23sp"
        android:textStyle="bold"
        app:fontFamily="@font/roboto"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/add_new_note_button_home_screen"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:background="@color/transparent_color"
        android:fontFamily="@font/roboto"
        android:src="@drawable/add_note_button_home_screen"
        android:text="@string/save"
        android:textAllCaps="false"
        android:textColor="@color/grey_text"
        android:textSize="14sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_goneMarginEnd="16dp"
        app:layout_goneMarginTop="24sp" />

    <androidx.appcompat.widget.AppCompatImageButton
        android:id="@+id/back_button_new_note_screen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:background="@color/transparent_color"
        android:src="@drawable/back_button"
        app:layout_constraintBottom_toBottomOf="@+id/mine_notes_tittle_text_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/mine_notes_tittle_text_view" />

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/new_note_card_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="12dp"
        android:backgroundTint="@color/lilac_color"
        android:clickable="true"
        android:focusable="true"
        android:theme="@style/CustomCardCorners"
        app:cardElevation="4dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/back_button_new_note_screen">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/newNote_cardTittle"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="@string/note_title"
                android:textColor="@color/black"
                android:textSize="14sp"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/newNote_cardDescription"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="12dp"
                android:text="@string/description"
                android:textColor="@color/grey_text"
                android:textSize="12sp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/newNote_cardTittle" />
        </androidx.constraintlayout.widget.ConstraintLayout>

    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/tittle_input_text_new_card_screen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        android:text="@string/edit_text_tittle"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="@+id/new_note_card_item"
        app:layout_constraintTop_toBottomOf="@id/new_note_card_item" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/new_note_input_tittle_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:hint="@string/type_your_tittle"
        app:boxStrokeColor="@color/grey_text"
        app:counterEnabled="true"
        app:counterMaxLength="25"
        app:hintTextColor="@color/grey_text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tittle_input_text_new_card_screen">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/white"
            app:layout_constraintStart_toStartOf="@+id/tittle_input_text_new_card_screen"
            app:layout_constraintTop_toBottomOf="@id/tittle_input_text_new_card_screen" />

    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/description_input_text_new_card_screen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        android:text="@string/description_new_note_screen_tittle"
        android:textSize="12sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@id/new_note_input_tittle_edit_text"
        app:layout_constraintTop_toBottomOf="@id/new_note_input_tittle_edit_text" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/new_note_input_description_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:hint="@string/type_your_description"
        app:boxStrokeColor="@color/grey_text"
        app:counterEnabled="true"
        app:counterMaxLength="220"
        app:hintTextColor="@color/grey_text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/description_input_text_new_card_screen">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@id/description_input_text_new_card_screen"
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:backgroundTint="@color/white"
            android:gravity="top|start"
            android:inputType="textMultiLine"
            android:minLines="2"
            app:layout_constraintStart_toStartOf="@+id/tittle_input_text_new_card_screen"
            app:layout_constraintTop_toBottomOf="@id/tittle_input_text_new_card_screen" />

    </com.google.android.material.textfield.TextInputLayout>

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/new_note_baby_blue_button"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginStart="16dp"
        android:background="@drawable/background_button_not_selected"
        android:backgroundTint="@color/blue_baby_color"
        android:backgroundTintMode="screen"
        android:focusable="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/new_note_input_description_edit_text" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/new_note_light_turquoise_button"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginStart="8dp"
        android:background="@drawable/background_button_not_selected"
        android:backgroundTint="@color/turquoise_color"
        android:backgroundTintMode="screen"
        app:layout_constraintStart_toEndOf="@id/new_note_baby_blue_button"
        app:layout_constraintTop_toBottomOf="@+id/new_note_input_description_edit_text" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/new_note_pastel_purple_button"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginStart="8dp"
        android:background="@drawable/background_button_not_selected"
        android:backgroundTint="@color/purple_color"
        android:backgroundTintMode="screen"
        app:layout_constraintStart_toEndOf="@id/new_note_light_turquoise_button"
        app:layout_constraintTop_toBottomOf="@+id/new_note_input_description_edit_text" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/new_note_pastel_lilac_button"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginStart="8dp"
        android:background="@drawable/background_button_not_selected"
        android:backgroundTint="@color/lilac_color"
        android:backgroundTintMode="screen"
        app:layout_constraintStart_toEndOf="@id/new_note_pastel_purple_button"
        app:layout_constraintTop_toBottomOf="@+id/new_note_input_description_edit_text" />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/new_note_pastel_pink_button"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginStart="8dp"
        android:background="@drawable/background_button_not_selected"
        android:backgroundTint="@color/pink_color"
        android:backgroundTintMode="screen"
        app:layout_constraintStart_toEndOf="@id/new_note_pastel_lilac_button"
        app:layout_constraintTop_toBottomOf="@+id/new_note_input_description_edit_text" />



</androidx.constraintlayout.widget.ConstraintLayout>

my fragment.kt:

package com.example.notesapp.home.presentation.view

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.notesapp.R
import com.example.notesapp.databinding.FragmentNewNoteScreenBinding

class NewNoteScreen : Fragment(R.layout.fragment_new_note_screen) {
    private lateinit var binding: FragmentNewNoteScreenBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {

        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentNewNoteScreenBinding.inflate(layoutInflater, container, false)

        setListener()

        return binding.root
    }

    private fun setListener() {
        binding.run {
            backButtonNewNoteScreen.setOnClickListener {
                findNavController().navigate(R.id.action_newNoteScreen_to_homeScreenfragment)
            }
        }
    }

    companion object {

    }
}

我想知道为什么会出现这个错误,以及如何解决它,这样我就可以继续编写我的应用程序。

a9wyjsp7

a9wyjsp71#

您遇到的错误是由于将同一IDdescription_input_text_new_card_screen分配给XML布局中的MaterialTextView和TextInputEditText而导致的。这会导致在尝试膨胀布局时出现类强制转换异常。
要解决此问题,您需要为这两个元素分配不同的ID。将XML布局中的MaterialTextView或TextInputEditText的ID更新为唯一ID。
例如,您可以将TextInputEditText的ID更改为new_note_input_text_edit:

相关问题