com.google.android.material.textview.MaterialTextView cannot be cast to com.google.android.material.textfield.TextInputEditText

huangapple go评论138阅读模式
英文:

com.google.android.material.textview.MaterialTextView cannot be cast to com.google.android.material.textfield.TextInputEditText

问题

你在尝试在Fragment中使用导航组件,但遇到了使用绑定(Binding)来膨胀(inflate)布局的问题。错误信息显示:

com.google.android.material.textview.MaterialTextView 无法转换为 com.google.android.material.textfield.TextInputEditText

这个错误的原因是你在布局中使用了MaterialTextView,但在TextInputLayout中却尝试将其转换为TextInputEditText

要解决这个问题,你可以在布局文件中将MaterialTextView替换为TextInputEditText

具体来说,将以下代码:

<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.TextInputEditText
    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:hint="@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" />

这样应该能解决你遇到的问题。如果还有其他疑问,请随时提出。

英文:

I am trying to use navigation components in my fragment but i am having problem inflating my layout using Binding.
see, this is my fragment xml and this is the fragment.kt code:

this is the error:

> com.google.android.material.textview.MaterialTextView cannot be cast
> to com.google.android.material.textfield.TextInputEditText

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:background=&quot;@color/background_white_color&quot;
tools:context=&quot;.home.presentation.view.EditNoteScreen&quot;&gt;
&lt;com.google.android.material.textview.MaterialTextView
android:id=&quot;@+id/mine_notes_tittle_text_view&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;58dp&quot;
android:layout_marginTop=&quot;18dp&quot;
android:fontFamily=&quot;@font/roboto&quot;
android:text=&quot;@string/new_note&quot;
android:textColor=&quot;@color/grey_text&quot;
android:textSize=&quot;23sp&quot;
android:textStyle=&quot;bold&quot;
app:fontFamily=&quot;@font/roboto&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;androidx.appcompat.widget.AppCompatButton
android:id=&quot;@+id/add_new_note_button_home_screen&quot;
android:layout_width=&quot;36dp&quot;
android:layout_height=&quot;36dp&quot;
android:layout_marginTop=&quot;16dp&quot;
android:layout_marginEnd=&quot;16dp&quot;
android:background=&quot;@color/transparent_color&quot;
android:fontFamily=&quot;@font/roboto&quot;
android:src=&quot;@drawable/add_note_button_home_screen&quot;
android:text=&quot;@string/save&quot;
android:textAllCaps=&quot;false&quot;
android:textColor=&quot;@color/grey_text&quot;
android:textSize=&quot;14sp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
app:layout_goneMarginEnd=&quot;16dp&quot;
app:layout_goneMarginTop=&quot;24sp&quot; /&gt;
&lt;androidx.appcompat.widget.AppCompatImageButton
android:id=&quot;@+id/back_button_new_note_screen&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;16dp&quot;
android:background=&quot;@color/transparent_color&quot;
android:src=&quot;@drawable/back_button&quot;
app:layout_constraintBottom_toBottomOf=&quot;@+id/mine_notes_tittle_text_view&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;@+id/mine_notes_tittle_text_view&quot; /&gt;
&lt;com.google.android.material.card.MaterialCardView
android:id=&quot;@+id/new_note_card_item&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;16dp&quot;
android:layout_marginTop=&quot;24dp&quot;
android:layout_marginEnd=&quot;16dp&quot;
android:layout_marginBottom=&quot;12dp&quot;
android:backgroundTint=&quot;@color/lilac_color&quot;
android:clickable=&quot;true&quot;
android:focusable=&quot;true&quot;
android:theme=&quot;@style/CustomCardCorners&quot;
app:cardElevation=&quot;4dp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/back_button_new_note_screen&quot;&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;androidx.appcompat.widget.AppCompatTextView
android:id=&quot;@+id/newNote_cardTittle&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;@string/note_title&quot;
android:textColor=&quot;@color/black&quot;
android:textSize=&quot;14sp&quot;
android:textStyle=&quot;bold&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;androidx.appcompat.widget.AppCompatTextView
android:id=&quot;@+id/newNote_cardDescription&quot;
android:layout_width=&quot;0dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;8dp&quot;
android:layout_marginBottom=&quot;12dp&quot;
android:text=&quot;@string/description&quot;
android:textColor=&quot;@color/grey_text&quot;
android:textSize=&quot;12sp&quot;
app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/newNote_cardTittle&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
&lt;/com.google.android.material.card.MaterialCardView&gt;
&lt;com.google.android.material.textview.MaterialTextView
android:id=&quot;@+id/tittle_input_text_new_card_screen&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;16dp&quot;
android:layout_marginTop=&quot;24dp&quot;
android:text=&quot;@string/edit_text_tittle&quot;
android:textSize=&quot;12sp&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/new_note_card_item&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/new_note_card_item&quot; /&gt;
&lt;com.google.android.material.textfield.TextInputLayout
android:id=&quot;@+id/new_note_input_tittle_edit_text&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;16dp&quot;
android:layout_marginTop=&quot;8dp&quot;
android:layout_marginEnd=&quot;16dp&quot;
android:hint=&quot;@string/type_your_tittle&quot;
app:boxStrokeColor=&quot;@color/grey_text&quot;
app:counterEnabled=&quot;true&quot;
app:counterMaxLength=&quot;25&quot;
app:hintTextColor=&quot;@color/grey_text&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/tittle_input_text_new_card_screen&quot;&gt;
&lt;com.google.android.material.textfield.TextInputEditText
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:backgroundTint=&quot;@color/white&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/tittle_input_text_new_card_screen&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/tittle_input_text_new_card_screen&quot; /&gt;
&lt;/com.google.android.material.textfield.TextInputLayout&gt;
&lt;com.google.android.material.textview.MaterialTextView
android:id=&quot;@+id/description_input_text_new_card_screen&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;16dp&quot;
android:layout_marginTop=&quot;24dp&quot;
android:text=&quot;@string/description_new_note_screen_tittle&quot;
android:textSize=&quot;12sp&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;@id/new_note_input_tittle_edit_text&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/new_note_input_tittle_edit_text&quot; /&gt;
&lt;com.google.android.material.textfield.TextInputLayout
android:id=&quot;@+id/new_note_input_description_edit_text&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginStart=&quot;16dp&quot;
android:layout_marginTop=&quot;8dp&quot;
android:layout_marginEnd=&quot;16dp&quot;
android:hint=&quot;@string/type_your_description&quot;
app:boxStrokeColor=&quot;@color/grey_text&quot;
app:counterEnabled=&quot;true&quot;
app:counterMaxLength=&quot;220&quot;
app:hintTextColor=&quot;@color/grey_text&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/description_input_text_new_card_screen&quot;&gt;
&lt;com.google.android.material.textfield.TextInputEditText
android:id=&quot;@id/description_input_text_new_card_screen&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;120dp&quot;
android:backgroundTint=&quot;@color/white&quot;
android:gravity=&quot;top|start&quot;
android:inputType=&quot;textMultiLine&quot;
android:minLines=&quot;2&quot;
app:layout_constraintStart_toStartOf=&quot;@+id/tittle_input_text_new_card_screen&quot;
app:layout_constraintTop_toBottomOf=&quot;@id/tittle_input_text_new_card_screen&quot; /&gt;
&lt;/com.google.android.material.textfield.TextInputLayout&gt;
&lt;androidx.appcompat.widget.AppCompatButton
android:id=&quot;@+id/new_note_baby_blue_button&quot;
android:layout_width=&quot;32dp&quot;
android:layout_height=&quot;32dp&quot;
android:layout_marginStart=&quot;16dp&quot;
android:background=&quot;@drawable/background_button_not_selected&quot;
android:backgroundTint=&quot;@color/blue_baby_color&quot;
android:backgroundTintMode=&quot;screen&quot;
android:focusable=&quot;true&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/new_note_input_description_edit_text&quot; /&gt;
&lt;androidx.appcompat.widget.AppCompatButton
android:id=&quot;@+id/new_note_light_turquoise_button&quot;
android:layout_width=&quot;32dp&quot;
android:layout_height=&quot;32dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:background=&quot;@drawable/background_button_not_selected&quot;
android:backgroundTint=&quot;@color/turquoise_color&quot;
android:backgroundTintMode=&quot;screen&quot;
app:layout_constraintStart_toEndOf=&quot;@id/new_note_baby_blue_button&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/new_note_input_description_edit_text&quot; /&gt;
&lt;androidx.appcompat.widget.AppCompatButton
android:id=&quot;@+id/new_note_pastel_purple_button&quot;
android:layout_width=&quot;32dp&quot;
android:layout_height=&quot;32dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:background=&quot;@drawable/background_button_not_selected&quot;
android:backgroundTint=&quot;@color/purple_color&quot;
android:backgroundTintMode=&quot;screen&quot;
app:layout_constraintStart_toEndOf=&quot;@id/new_note_light_turquoise_button&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/new_note_input_description_edit_text&quot; /&gt;
&lt;androidx.appcompat.widget.AppCompatButton
android:id=&quot;@+id/new_note_pastel_lilac_button&quot;
android:layout_width=&quot;32dp&quot;
android:layout_height=&quot;32dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:background=&quot;@drawable/background_button_not_selected&quot;
android:backgroundTint=&quot;@color/lilac_color&quot;
android:backgroundTintMode=&quot;screen&quot;
app:layout_constraintStart_toEndOf=&quot;@id/new_note_pastel_purple_button&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/new_note_input_description_edit_text&quot; /&gt;
&lt;androidx.appcompat.widget.AppCompatButton
android:id=&quot;@+id/new_note_pastel_pink_button&quot;
android:layout_width=&quot;32dp&quot;
android:layout_height=&quot;32dp&quot;
android:layout_marginStart=&quot;8dp&quot;
android:background=&quot;@drawable/background_button_not_selected&quot;
android:backgroundTint=&quot;@color/pink_color&quot;
android:backgroundTintMode=&quot;screen&quot;
app:layout_constraintStart_toEndOf=&quot;@id/new_note_pastel_lilac_button&quot;
app:layout_constraintTop_toBottomOf=&quot;@+id/new_note_input_description_edit_text&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

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 {
}
}

I would like to know why the error is and how to resolve it so I can continue coding my application.

答案1

得分: 0

你遇到的错误是因为在你的XML布局中为MaterialTextView和TextInputEditText分配了相同的ID description_input_text_new_card_screen,导致在尝试膨胀布局时出现类强制转换异常。

要解决这个问题,你需要为这两个元素分配不同的ID。在你的XML布局中,将MaterialTextView或TextInputEditText的ID更新为唯一的ID。

例如,你可以将TextInputEditText的ID更改为new_note_input_text_edit:

英文:

The error you are encountering is caused by assigning the same ID, description_input_text_new_card_screen, to both a MaterialTextView and a TextInputEditText in your XML layout. This leads to a class cast exception when trying to inflate the layout.

To resolve this issue, you need to assign different IDs to these two elements. Update the ID of either the MaterialTextView or the TextInputEditText in your XML layout to a unique ID.

For example, you can change the ID of the TextInputEditText to new_note_input_text_edit:

huangapple
  • 本文由 发表于 2023年5月26日 07:59:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76336881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定