WrongViewCast错误:意外隐式转换为TabLayout:布局标记为线性布局

时间:2019-11-23 19:49:39

标签: android

我正在尝试复制以前使用的选项卡布局版本,但是由于库依赖关系发生了更改,因此出现了意外错误。在我的xml中,我有:

<com.google.android.material.tabs.TabLayout
        android:id="@id/tabLayout"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/nextButton"
        android:layout_marginEnd="15dp"
        android:layout_marginStart="20dp"
        android:layout_marginBottom="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1st" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2nd" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3rd" />
    </com.google.android.material.tabs.TabLayout>

,它在约束布局内。当我尝试通过ID查找此视图时:

TabLayout tabLayout = findViewById(R.id.tabLayout);

我收到WrongViewCast错误。 import com.google.android.material.tabs.TabLayout;我导入了正确的库。在TabLayout上下文中调用findViewById的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

android:id="@id/tabLayout"更改为android:id="@+id/tabLayout"。 。