0

I upgraded gradle from

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

to

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

This resulted in the following error.

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

This happens when the strPwd is set but not when the strUsername is set.

Files:

<?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();
    }

Why does this error occur and how to fix this.

2 Answers2

0

If you using compileSdkVersion 27 add this:

implementation 'com.android.support:design:27.1.1'
Paraskevas Ntsounos
  • 1,755
  • 2
  • 18
  • 34
0

You might be casting TextInputLayout with TextInputEditText in your Java findViewById

You should use something like this,

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

Use this. Also post your Java and XML too.

Naveen T P
  • 6,955
  • 2
  • 22
  • 29