簡體   English   中英

Gradle構建工具的升級結果在ClassCastException中

[英]Gradle build tools upgrade results in ClassCastException

我從升級了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的

私有最終靜態int INDEX_USERNAME = 0; 私有最終靜態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'

你可能會鑄造TextInputLayoutTextInputEditText在Java findViewById

你應該用這樣的東西

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

用這個。 還要發布您的Java和XML。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM