java - Gradle 构建工具升级导致 ClassCastException

标签 java android gradle android-gradle-plugin

我从

classpath 'com.android.tools.build:gradle:2.3.3'


classpath 'com.android.tools.build:gradle:3.1.2'

这导致了以下错误。

java.lang.ClassCastException: android.support.design.widget.TextInputLayout cannot be cast to android.support.design.widget.TextInputEditText



当设置了 strPwd 而不是设置了 strUsername 时会发生这种情况。

文件:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:background="?android:colorBackground">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/section_spacer">

        <include layout="@layout/layout_list_item_input"
            android:id="@+id/username" />

        <include layout="@layout/layout_list_item_input"
            android:id="@+id/password" />

        <include layout="@layout/spacer" />

        <include layout="@layout/button_main"
            android:id="@+id/btn_sign_in" />

        <include layout="@layout/button_white"
            android:id="@+id/btn_forgot_password" />

    </LinearLayout>
</ScrollView>

layout_list_item_input.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:focusable="true"
    android:focusableInTouchMode="true">
    <!-- Note: Separator is focusable to enable manually removing focus from edit views when needed.
           Credit: https://stackoverflow.com/a/6118033/1291040
    -->

    <android.support.design.widget.TextInputLayout
        android:id="@+id/input_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="?android:attr/listPreferredItemPaddingLeft">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/input"
            android:maxLines="1"
            android:inputType="text"
            android:imeOptions="actionDone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceListItemSmall"
            android:gravity="center_vertical" />

    </android.support.design.widget.TextInputLayout>

    <View
        android:id="@+id/separator"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="?android:colorBackground" />

</LinearLayout>

java :

私有(private)最终静态 int INDEX_USERNAME = 0;
私有(private)最终静态 int INDEX_PASSWORD = 1;
 final String strUsername = getListItemEditTextValue(username, INDEX_USERNAME);
 final String strPwd = getListItemEditTextValue(password, INDEX_PASSWORD);

protected String getListItemEditTextValue(View view, int index) {
        return ((TextInputEditText) view.findViewById(R.id.input + index)).getText().toString();
    }

为什么会发生此错误以及如何解决此问题。

最佳答案

如果你使用 compileSdkVersion 27 添加这个:

implementation 'com.android.support:design:27.1.1'

关于java - Gradle 构建工具升级导致 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50704261/

相关文章:

java - Hibernate 查询返回陈旧数据

android - Recyclerview 触摸事件

Android 使用配置 VS 使用功能

android - 无法在Android设备上播放音频

java - 如何在jitpack中发布Java库?我尝试了一次尝试但是没有成功

java - 设置 Docker 来处理 Maven 和 Gradle?

android - Instant run 不适用于 Android Studio,表示需要 Gradle 版本 2.10。我用2.10

java - 如何解析数据集apache spark java中的多行json

java - PagerAdapter 中的 ImageButton onclick。需要转到另一个类/XML 文件。错误

java - 迄今为止最好的 Java 反混淆工具 - [问题已关闭]