android.support.v7.widget.AppCompatTextView 无法转换为 android.widget.ImageView

android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.ImageView

这是 activity_main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.issaayoub.materialtest.MainActivity">

    <include
        android:id="@+id/app_bar"
        layout="@layout/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/activity_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                android:elevation="4dp">

                <android.support.design.widget.TabItem
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:icon="@drawable/an"
                    android:text="@string/bird1" />

            </android.support.design.widget.TabLayout>

            <android.support.v4.view.ViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/tabLayout">

            </android.support.v4.view.ViewPager>
        </RelativeLayout>

        <fragment
            android:id="@+id/fragment_navigation_drawer"
            android:name="com.issaayoub.materialtest.NavigationDrawerFragment"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer"
            tools:layout="@layout/fragment_navigation_drawer" />

    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

每当我在 TabLayout 中添加当前 TabItem 时,我都会收到以下错误:

android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.ImageView

打开渲染问题的详细信息时得到如下信息:

java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.ImageView
    at android.support.design.widget.TabLayout$TabView.update(TabLayout.java:1675)
    at android.support.design.widget.TabLayout$TabView.setTab(TabLayout.java:1627)
    at android.support.design.widget.TabLayout.createTabView(TabLayout.java:927)
    at android.support.design.widget.TabLayout.newTab(TabLayout.java:562)
    at android.support.design.widget.TabLayout.addTabFromItemView(TabLayout.java:481)
    at android.support.design.widget.TabLayout.addViewInternal(TabLayout.java:970)
    at android.support.design.widget.TabLayout.addView(TabLayout.java:960)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:862)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:834)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:861)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:834)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:861)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:834)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:861)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:834)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:324)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:389)
    at com.android.tools.idea.rendering.RenderTask.compute(RenderTask.java:548)
    at com.android.tools.idea.rendering.RenderTask.compute(RenderTask.java:533)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:966)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:533)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:659)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

有人可以突出显示 TabItem 的问题是什么。

删除 TabItem 将消除所有此错误。最后请注意,应用程序是运行正常没有任何错误就phone/emulator。

感谢帮助!!

<android.support.design.widget.TabLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <android.support.design.widget.TabItem
        android:text="@string/tab_text"/>

    <android.support.design.widget.TabItem
        android:icon="@drawable/ic_android"/>

</android.support.design.widget.TabLayout>

Android Devp website给出的代码,我觉得这里只能给出文字或者图标,不能同时给出。

试试吧。

没有成功。我重新启动了 Android Studio,错误已删除。
请注意,我们可以同时添加文本和图标。 这是应用程序布局的图片: