Я пытаюсь использовать EditText из Material Design - и я получаю исключение во время выполнения - PullRequest
0 голосов
/ 09 апреля 2020

Я пытаюсь использовать EditText из Material Design (https://material.io/develop/android/components/text-fields/) и получаю исключение во время выполнения.

Я хочу решить эту проблему

EditText Проблема: Когда я пытаюсь открыть приложение с обычным EditText на мониторе и приложение Cra sh [закрыто]

Это часть журнала выполнения:

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)

/ full

    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>

Пожалуйста, мне нужна помощь

1 Ответ

0 голосов
/ 09 апреля 2020

Также та же ошибка

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);

Вы должны дать идентификатор 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...