问题描述
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作为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>
recover_password_sheet.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" />
解决方法
您的问题出在以下部分:
R.id.sheet_container as LinearLayout
您正在尝试将整数(代表资源)转换为LinearLayout
。
请查看LayoutInflater上的文档,以找出inflate
函数的哪些重载更适合您的需求。
问题是XML文件和.kt文件
固定的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<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:background="@color/colorWhiteDivider"
android:layout_width = "match_parent"
android:layout_height="1dp"/>
<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:background="@color/colorWhiteDivider"
android:layout_marginTop="50dp"
android:layout_width = "match_parent"
android:layout_height="1dp"/>
<Button
android:id="@+id/btn_accept"
android:layout_marginTop="20dp"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="20dp"
android:layout_gravity="center"
android:background="@drawable/custom_rounded_corner_icons"
android:textColor="@color/colorWhite"
android:textSize="12sp"
android:text="@string/accept"
android:textAlignment="center"
tools:ignore="RtlCompat" />
</LinearLayout>
.kt文件
var bottomSheetDialog = BottomSheetDialog(this)
var view = layoutInflater.inflate(R.layout.recover_password_sheet,null)
对我有用