【问题标题】:java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextViewjava.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView 不能转换为 android.widget.TextView
【发布时间】:2021-12-14 15:14:00
【问题描述】:

我在启动我的活动时不断收到运行时错误,它说 androidx.appcompat.widget.AppCompatImageView 无法转换为 android.widget.TextView?

    Process: com.jaygohel.ubereatsclone, PID: 5010
    java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to android.widget.TextView
        at com.jaygohel.ubereatsclone.adapter.HomeAdapter$viewHolder.<init>(HomeAdapter.java:50)
        at com.jaygohel.ubereatsclone.adapter.HomeAdapter.onCreateViewHolder(HomeAdapter.java:31)
        at com.jaygohel.ubereatsclone.adapter.HomeAdapter.onCreateViewHolder(HomeAdapter.java:17)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:561)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:16953)
        at android.view.ViewGroup.layout(ViewGroup.java:5579)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1873)
        at android.view.View.layout(View.java:16953)
        at android.view.ViewGroup.layout(ViewGroup.java:5579)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
        at android.view.View.layout(View.java:16953)
        at android.view.ViewGroup.layout(ViewGroup.java:5579)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2001)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1844)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1753)
        at android.view.View.layout(View.java:16953)
        at android.view.ViewGroup.layout(ViewGroup.java:5579)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
        at android.view.View.layout(View.java:16953)
        at android.view.ViewGroup.layout(ViewGroup.java:5579)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2001)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1844)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1753)
        at android.view.View.layout(View.java:16953)
        at android.view.ViewGroup.layout(ViewGroup.java:5579)
        at android.app.ActivityThread.main(ActivityThre
I/Process: Sending signal. PID: 5010 SIG: 9

主Activity.xml

<?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"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/listProduct"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> 

listitem.xml

<?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"
    tools:context=".MainActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/listProduct"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我也试图清理项目重新构建它。但这也行不通。我无法取消项目中将产生什么问题。

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
  • 您能告诉我们这个问题与 Firebase 有什么关系吗?

标签: java android android-layout android-x86


【解决方案1】:

没有看到你的 homeAdapter 类很难说。无论如何,它可能是由错误的 id 引起的。

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
猜你喜欢
  • 2019-05-08
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多