我正在尝试使用 Material Design 中的 EditText - 我遇到了运行时异常

I'm trying to use EditText from Material Design - and I'm getting runtime exception

我正在尝试使用 Material Design ( https://material.io/develop/android/components/text-fields/ ) 中的 EditText,但出现 运行 时间异常。

我想解决这个问题

EditText 问题:当我尝试在监视器中打开带有常规 EditText 的应用程序时,应用程序崩溃 [关闭]

这是 运行 日志的一部分:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fares.orderfood/com.fares.orderfood.SignInActivity}: java.lang.ClassCastException: com.google.android.material.textfield.TextInputLayout cannot be cast to android.widget.EditText

    at com.fares.orderfood.SignInActivity.onCreate(SignInActivity.java:33)

/满

    E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fares.orderfood, PID: 5512
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fares.orderfood/com.fares.orderfood.SignInActivity}: java.lang.ClassCastException: com.google.android.material.textfield.TextInputLayout cannot be cast to android.widget.EditText
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.ClassCastException: com.google.android.material.textfield.TextInputLayout cannot be cast to android.widget.EditText
    at com.fares.orderfood.SignInActivity.onCreate(SignInActivity.java:34)
    at android.app.Activity.performCreate(Activity.java:7009)

我的java第33行

edtName =(EditText)findViewById(R.id.edtName);

我的 java 文件已满:

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.fares.orderfood.model.User;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public
class SignInActivity extends AppCompatActivity {

EditText edtName, edtphone,edtpassword;
Button signinbtn1,cancel1;

@Override
protected
void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in);

////////////////Text Input///////////////////
    edtName =(EditText)findViewById(R.id.edtName);     /// this line (33)

    edtpassword = (EditText)findViewById(R.id.edtpassword);
    edtphone = (EditText)findViewById(R.id.edtphone);

我的样式文件

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

<item name="colorControlActivated">@color/colorPrimary</item>

             <item name="textAppearanceHeadline1">@style/TextAppearance.MaterialComponents.Headline1</item>
             <item name="textAppearanceHeadline2">@style/TextAppearance.MaterialComponents.Headline2</item>
             <item name="textAppearanceHeadline3">@style/TextAppearance.MaterialComponents.Headline3</item>
             <item name="textAppearanceHeadline4">@style/TextAppearance.MaterialComponents.Headline4</item>
             <item name="textAppearanceHeadline5">@style/TextAppearance.MaterialComponents.Headline5</item>
             <item name="textAppearanceHeadline6">@style/TextAppearance.MaterialComponents.Headline6</item>
             <item name="textAppearanceSubtitle1">@style/TextAppearance.MaterialComponents.Subtitle1</item>
             <item name="textAppearanceSubtitle2">@style/TextAppearance.MaterialComponents.Subtitle2</item>
             <item name="textAppearanceBody1">@style/TextAppearance.MaterialComponents.Body1</item>
             <item name="textAppearanceBody2">@style/TextAppearance.MaterialComponents.Body2</item>
             <item name="textAppearanceCaption">@style/TextAppearance.MaterialComponents.Caption</item>
             <item name="textAppearanceButton">@style/TextAppearance.MaterialComponents.Button</item>
             <item name="textAppearanceOverline">@style/TextAppearance.MaterialComponents.Overline</item>
         <!--  // Librarty material.io/design // -->

    </style>

添加新的我的 xml 这是我的 xml

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/loginPageBackgroundColor"
    tools:context=".SignInActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:orientation="vertical"
        android:padding="24dp"
        android:paddingTop="16dp">

        <ImageView
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="48dp"
            android:layout_marginBottom="16dp"
            app:srcCompat="@drawable/store"
            tools:ignore="VectorDrawableCompat" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="122dp"
            android:text="@string/signin"
            android:textSize="50sp"
            android:textStyle="bold" />

    <!-- input Username  -->
        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_name">
                <com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="text"
                    android:maxLines="1"
                    android:text="fares"/>
        </com.google.android.material.textfield.TextInputLayout>
        <!-- input Username  -->
        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtphone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_phone">
            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="phone"
                android:maxLines="1"
                android:text="0988123344"/>
        </com.google.android.material.textfield.TextInputLayout>

    <!-- input Password  -->
        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/edtpassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_password"
            app:errorEnabled="true"
            app:endIconMode="password_toggle">
            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:text="1234"/>
        </com.google.android.material.textfield.TextInputLayout>


    <!-- input Button  -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- input Button signin -->
        <com.google.android.material.button.MaterialButton
            android:id="@+id/signbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:text="@string/signin"
            app:backgroundTint="#A8A8A8"/>

        <!-- input Button cancel -->
        <com.google.android.material.button.MaterialButton
        android:id="@+id/cancel_button"
        style="@style/Widget.MaterialComponents.Button.TextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="12dp"
        android:layout_marginRight="12dp"

        android:layout_toStartOf="@+id/signbtn"
        android:layout_toLeftOf="@+id/signbtn"
        android:text="@string/text_cancel" />
    </RelativeLayout>



    </LinearLayout>

</ScrollView>

拜托,我需要帮助

同样的错误

You are casting TextInputLayout into EditText which is not possible, instead of this please try to cast TextInputEditText into EditText it will resolve your problem.

我的java代码

public
class SignInActivity extends AppCompatActivity {

    TextInputEditText edtName, edtphone,edtpassword;
    Button signinbtn1,cancel1;

    @Override
    protected
    void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);

    ////////////////Text Input///////////////////
        edtName =(TextInputEditText)findViewById(R.id.edtName);

        edtpassword = (TextInputEditText)findViewById(R.id.edtpassword);
        edtphone = (TextInputEditText)findViewById(R.id.edtphone);

您必须像这样将 id 提供给 TextInputEditText 而不是 TextInputLayout。它将解决您的问题:

XML布局

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/loginPageBackgroundColor"
    tools:context=".SignInActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:orientation="vertical"
        android:padding="24dp"
        android:paddingTop="16dp">

        <ImageView
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="48dp"
            android:layout_marginBottom="16dp"
            app:srcCompat="@drawable/store"
            tools:ignore="VectorDrawableCompat" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="122dp"
            android:text="@string/signin"
            android:textSize="50sp"
            android:textStyle="bold" />

    <!-- input Username  -->
        <com.google.android.material.textfield.TextInputLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_name">
                <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edtName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="text"
                    android:maxLines="1"
                    android:text="fares"/>
        </com.google.android.material.textfield.TextInputLayout>
        <!-- input Username  -->
        <com.google.android.material.textfield.TextInputLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_phone">
            <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edtphone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="phone"
                android:maxLines="1"
                android:text="0988123344"/>
        </com.google.android.material.textfield.TextInputLayout>

    <!-- input Password  -->
        <com.google.android.material.textfield.TextInputLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_password"
            app:errorEnabled="true"
            app:endIconMode="password_toggle">
            <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/edtpassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:text="1234"/>
        </com.google.android.material.textfield.TextInputLayout>


    <!-- input Button  -->
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- input Button signin -->
        <com.google.android.material.button.MaterialButton
            android:id="@+id/signbtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:text="@string/signin"
            app:backgroundTint="#A8A8A8"/>

        <!-- input Button cancel -->
        <com.google.android.material.button.MaterialButton
        android:id="@+id/cancel_button"
        style="@style/Widget.MaterialComponents.Button.TextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="12dp"
        android:layout_marginRight="12dp"

        android:layout_toStartOf="@+id/signbtn"
        android:layout_toLeftOf="@+id/signbtn"
        android:text="@string/text_cancel" />
    </RelativeLayout>



    </LinearLayout>

</ScrollView>