英文:
I'm trying to use EditText from Material Design - and I'm getting runtime exception
问题
I'm trying to use EditText from Material Design (https://material.io/develop/android/components/text-fields/) and I'm getting a runtime exception.
I want to solve this problem
EditText Problem: When I try to open the app with a regular EditText on a monitor, the app crashes [closed]
This is part of the run log:
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)
My Java line 33:
edtName = (EditText) findViewById(R.id.edtName);
My Java file full:
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);
}
}
My style file:
<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>
<!-- // Library material.io/design // -->
</style>
Add my new 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
<details>
<summary>英文:</summary>
I'm trying to use EditText from Material Design ( https://material.io/develop/android/components/text-fields/ ) and I'm getting runtime exception.
I want to solve this problem
EditText Problem : When i try to open app with a regular EditText in a monitor and the app Crash [closed]
This's part of run log:
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)
my java line 33
edtName =(EditText)findViewById(R.id.edtName);
My java file full:
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);
my style file
<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>
add new my xml
this my 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>
Please, I need help
</details>
# 答案1
**得分**: 0
同样的错误:
您正在将TextInputLayout转换为EditText,这是不可能的。请尝试将TextInputEditText转换为EditText,这将解决您的问题。
我的Java代码:
```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);
////////////////文本输入///////////////////
edtName = (TextInputEditText) findViewById(R.id.edtName);
edtpassword = (TextInputEditText) findViewById(R.id.edtpassword);
edtphone = (TextInputEditText) findViewById(R.id.edtphone);
}
}
您需要像这样给TextInputEditText分配ID,而不是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" />
<!-- 输入用户名 -->
<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>
<!-- 输入手机号 -->
<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>
<!-- 输入密码 -->
<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>
<!-- 输入按钮 -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 登录按钮 -->
<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" />
<!-- 取消按钮 -->
<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>
英文:
Also the same error
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.
my java code
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);
You have to give the id to TextInputEditText like this instead of TextInputLayout. It will resolve your problem:
XML layout
<?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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论