Android的java.lang.ClassCastException:android.widget.RelativeLayout不能转换为android.widget.EditText [英] Android java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText
问题描述
点击一个按钮,把我从一个活动到另一个当我在Android的以下异常(我在Android开发新的,所以它可能不是最亮的问题):
java.lang.ClassCastException:android.widget.RelativeLayout不能转换为android.widget.EditText
块引用>我曾尝试清洗项目好几次,我已经试过选项从Android的工具修复项目属性,我检查我的XML为错误,但我似乎无法弄清楚这一点。我不得不提及的是,按钮以及工作了一段时间,直到发生此异常。
下面是我的XML:
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / editCustomerPhone
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=@机器人:彩色/ background_dark
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=CustomerInfoActivity。><的FrameLayout
机器人:ID =@ + ID / frameLayout1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =250dp
机器人:layout_alignParentLeft =真
机器人:layout_alignParentRight =真
机器人:layout_alignParentTop =真
机器人:背景=@绘制/ ic_new_delivery> <的TextView
机器人:ID =@ + ID / textView2
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:文字=插入客户资料:
机器人:文字颜色=@彩色/ light_blue
机器人:TEXTSIZE =20SP
机器人:TEXTSTYLE =斜体/>< /&的FrameLayout GT;<的EditText
机器人:ID =@ + ID / editCustomerName
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBaseline =@ + ID / textView1
机器人:layout_alignBottom =@ + ID / textView1
机器人:layout_alignParentRight =真
机器人:EMS =10
安卓的inputType =textPersonName
机器人:文字颜色=@色/ light_blue/><按钮
机器人:ID =@ + ID / submitInfoBtn
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID / textView1
机器人:layout_alignParentBottom =真
机器人:layout_alignRight =@ + ID / editCustomerName
机器人:文字=提交信息
机器人:文字颜色=@彩色/ light_red
安卓的onClick =submitCustomerInfo/><的EditText
机器人:ID =@ + ID / editCustomerPhone
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID / editCustomerName
机器人:layout_alignParentRight =真
机器人:layout_alignTop =@ + ID / TextView01
机器人:EMS =10
安卓的inputType =手机
机器人:文字颜色=@色/ light_red> < requestFocus的/>
< /&的EditText GT;<的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_below =@ + ID / frameLayout1
机器人:文字=姓名:
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文字颜色=@机器人:彩色/ background_light
机器人:文字样式=大胆
机器人:字体=衬/><的TextView
机器人:ID =@ + ID / TextView02
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_below =@ + ID / editCustomerPhone
机器人:layout_marginTop =14dp
机器人:文字=电子邮件:
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文字颜色=@机器人:彩色/ background_light
机器人:文字样式=大胆
机器人:字体=衬/><的EditText
机器人:ID =@ + ID / editCustomerEmail
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBaseline =@ + ID / TextView02
机器人:layout_alignBottom =@ + ID / TextView02
机器人:layout_alignLeft =@ + ID / editCustomerPhone
机器人:layout_alignParentRight =真
机器人:EMS =10
安卓的inputType =textEmailAddress
机器人:文字颜色=@色/ light_green/><的TextView
机器人:ID =@ + ID / TextView01
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignLeft =@ + ID / TextView02
机器人:layout_below =@ + ID / editCustomerName
机器人:layout_marginTop =19dp
机器人:文字=电话:
机器人:textAppearance =机器人:ATTR / textAppearanceMedium
机器人:文字颜色=@机器人:彩色/ background_light
机器人:文字样式=大胆
机器人:字体=衬/>
下面是我在抛出异常的活动onCreate方法:
@覆盖
保护无效的onCreate(捆绑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); //异常点我在这里
}
解决方案在code说:
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / editCustomerPhone
editCustomerPhone
是一个RelativeLayout的。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):
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to 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.
Here is my 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:
@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 }
解决方案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.这篇关于Android的java.lang.ClassCastException:android.widget.RelativeLayout不能转换为android.widget.EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!