Gradle构建工具升级导致ClassCastException

时间:2018-06-05 15:51:21

标签: java android gradle android-gradle

我从

升级了gradle
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无法强制转换为   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 final static int INDEX_USERNAME = 0;     private final static 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();
    }

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您可能在TextInputLayout

中使用TextInputEditText投射findViewById

你应该使用这样的东西,

TextInputLayout textInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout);

使用它。同时发布您的Java和XML。