android - WrongViewCast 错误 : Unexpected implicit cast to TabLayout: layout tag was Linear Layout

标签 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 查找此 View 时:

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

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

最佳答案

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

关于android - WrongViewCast 错误 : Unexpected implicit cast to TabLayout: layout tag was Linear Layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59011596/

相关文章:

Android - 每 1 秒 SQL 查询 2 分钟

android - 保存和恢复 ExpandableListActivity 的展开/折叠状态

android - 操作栏选项卡的全息主题向后兼容性

android - 使用 android MediaCodec 对原始 H264 视频流的解码不正确

android - 无法从服务器连接到 Google API (401 : Unauthorized)

android - Google Play Dev Console 自动化创建新应用

Android 在方向更改后从 AsycTask 更新 UI

android - 由于未接受许可协议(protocol),run-android 无法运行

android - MVI架构中的单次事件

java - 使用 Activity 类在 fragment 类中调用方法