Результаты обновления инструментов сборки Gradle в ClassCastException - PullRequest
0 голосов
/ 05 июня 2018

Я обновил 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 голосов
/ 05 июня 2018

Возможно, вы используете TextInputLayout с TextInputEditText в вашей Java findViewById

Вы должны использовать что-то вроде этого,

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

Используйте это,Также опубликуйте свой Java и XML тоже.

0 голосов
/ 05 июня 2018

Если вы используете compileSdkVersion 27, добавьте:

implementation 'com.android.support:design:27.1.1'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...