Android java.lang.ClassCastException: android.widget.RelativeLayout 无法转换为 android.widget.EditText
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20951847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText
提问by barca_d
I get the following exception in Android when clicking a button to take me from an activity to another (I'm new in Android development, so it may not be the brightest of questions):
单击按钮将我从一个活动带到另一个活动时,我在 Android 中遇到以下异常(我是 Android 开发的新手,所以它可能不是最聪明的问题):
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText
java.lang.ClassCastException: android.widget.RelativeLayout 不能转换为 android.widget.EditText
I have tried cleaning the project several times, I've tried the option Fix Project Properties from Android Tools, I've checked my xml for mistakes but I just can't seem to figure this out. I have to mention that the button worked well for some time until this exception occurred.
我试过多次清理项目,我试过 Android 工具中的修复项目属性选项,我检查了我的 xml 是否有错误,但我似乎无法弄清楚这一点。我不得不提一下,在发生此异常之前,该按钮在一段时间内运行良好。
Here is my xml:
这是我的 xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/editCustomerPhone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CustomerInfoActivity" >
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/ic_new_delivery" >
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Insert Customer Data:"
android:textColor="@color/light_blue"
android:textSize="20sp"
android:textStyle="italic" />
</FrameLayout>
<EditText
android:id="@+id/editCustomerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="textPersonName"
android:textColor="@color/light_blue" />
<Button
android:id="@+id/submitInfoBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/editCustomerName"
android:text="Submit Info"
android:textColor="@color/light_red"
android:onClick="submitCustomerInfo"/>
<EditText
android:id="@+id/editCustomerPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editCustomerName"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/TextView01"
android:ems="10"
android:inputType="phone"
android:textColor="@color/light_red" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/frameLayout1"
android:text="Name:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/background_light"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editCustomerPhone"
android:layout_marginTop="14dp"
android:text="Email:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/background_light"
android:textStyle="bold"
android:typeface="serif" />
<EditText
android:id="@+id/editCustomerEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/TextView02"
android:layout_alignBottom="@+id/TextView02"
android:layout_alignLeft="@+id/editCustomerPhone"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="textEmailAddress"
android:textColor="@color/light_green" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextView02"
android:layout_below="@+id/editCustomerName"
android:layout_marginTop="19dp"
android:text="Phone:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/background_light"
android:textStyle="bold"
android:typeface="serif" />
Here is my onCreate method in the activity that throws the exception:
这是我在引发异常的活动中的 onCreate 方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_info);
this.editMail = (EditText)findViewById(R.id.editCustomerEmail);
this.editName = (EditText)findViewById(R.id.editCustomerName);
this.editPhone = (EditText)findViewById(R.id.editCustomerPhone); // the exception points me here
}
采纳答案by njzk2
you code says :
你的代码说:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/editCustomerPhone"
editCustomerPhone
is a relativelayout.
editCustomerPhone
是一个相对布局。
回答by Sush
android:id="@+id/editCustomerPhone"
android:id="@+id/editCustomerPhone"
remove this line
删除这一行
from below tag
从下面的标签
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/editCustomerPhone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_dark"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".CustomerInfoActivity" >
you have set it twice. One for relative layout one for edittext
你已经设置了两次。一种用于相对布局 一种用于edittext
回答by Adam M. Erickson
I received this error when my main activity.xmldid not have the include statement to my content.xmlfile.
当我的主要activity.xml没有包含我的content.xml文件的语句时,我收到此错误。
Once i added these lines back into my main activity.xmlfile, everything was fine again.
一旦我将这些行重新添加到我的主activity.xml文件中,一切又都好了。
<include
android:id="@+id/editText"
layout="@layout/content" />