我正在尝试复制以前工作过的选项卡布局版本,但是由于库依赖项发生了变化,我遇到了意外错误。在我的 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/