首页 > 解决方案 > java.lang.ClassCastException:java.lang.Integer 无法转换为 android.widget.LinearLayout 当我尝试充气时

问题描述

rerferf我正在尝试在 Kotlin https://www.youtube.com/watch?v=hfoXhiMTc0c中复制本教程, 但是当我制作膨胀部分时出现此错误:

java.lang.ClassCastException:java.lang.Integer 无法转换为 android.widget.LinearLayout

这是我的代码

var bottomSheetDialog = BottomSheetDialog(this, R.style.Theme_Design_BottomSheetDialog) var view: View = LayoutInflater.from(application).inflate(R.layout.recover_password_sheet, R.id.sheet_container as LinearLayout)

custom_bottom_sheet.xml

xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"> xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/colorWhite"/> <corners android:topLeftRadius="20dp" android:topRightRadius="20dp"/> </shape>

恢复密码表.xml

xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/sheet_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/custom_bottom_sheet" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="15dp" android:text="@string/forgot_password" android:textColor="@color/colorBlack" android:textStyle="bold"/> <view android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/colorWhiteDivider"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/forgot_password_description" android:padding="20dp" android:gravity="start"/> <com.google.android.material.textfield.TextInputLayout android:id="@+id/email" android:layout_marginStart="24dp" android:layout_marginTop="20dp" android:layout_marginEnd="24dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusableInTouchMode="true"> <androidx.appcompat.widget.AppCompatEditText android:id="@+id/txt_email" android:layout_width="match_parent" android:layout_height="50dp" android:background="@drawable/custom_rounded_login_text_view" android:drawableLeft="@drawable/icon_username" android:drawablePadding="10dp" android:hint="@string/prompt_email" android:inputType="textEmailAddress" android:paddingLeft="20dp" android:paddingRight="20dp" /> </com.google.android.material.textfield.TextInputLayout> <view android:layout_marginTop="50dp" android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/colorWhiteDivider" android:paddingTop="20dp" /> <Button android:id="@+id/btn_accept" android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginStart="20dp" android:layout_marginEnd="20dp" android:layout_marginBottom="20dp" android:background="@drawable/custom_rounded_corner_icons" android:textColor="@color/colorWhite" android:padding="20dp" android:gravity="start" android:textSize="15sp" android:text="@string/accept" android:textAlignment="center" tools:ignore="RtlCompat" />

标签: kotlinlayout-inflaterandroid-inflate

解决方案


您的问题在于:

R.id.sheet_container as LinearLayout

在那里,您尝试将整数(代表资源)转换为LinearLayout.

查看LayoutInflater上的文档,找出函数的哪些重载inflate更适合您的需求。


推荐阅读