简体   繁体   English

Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText

[英]Android java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText

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. 我试过几次清理项目,尝试过Android Tools中的Fix Project Properties选项,已经检查了xml中的错误,但似乎无法弄清楚。 I have to mention that the button worked well for some time until this exception occurred. 我不得不提到,在发生此异常之前,该按钮可以正常工作一段时间。

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
}

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是相对editCustomerPhone

android:id="@+id/editCustomerPhone" 机器人: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

I received this error when my main activity.xml did not have the include statement to my content.xml file. 当我的主activity.xml文件没有包含到content.xml文件的include语句时,我收到此错误。

Once i added these lines back into my main activity.xml file, everything was fine again. 一旦将这些行添加回我的主activity.xml文件中,一切就再好了。

    <include
    android:id="@+id/editText"
    layout="@layout/content" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为android.widget.EditText - java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText android.widget.RelativeLayout无法转换为android.widget.EditText - android.widget.RelativeLayout cannot be cast to android.widget.EditText java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.FrameLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.LinearLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams java.lang.ClassCastException:android.widget.TextView无法转换为android.widget.EditText - java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText java.lang.ClassCastException:android.view.ViewGroup$LayoutParams 不能转换为 android.widget.RelativeLayout$LayoutParams - java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams 原因:java.lang.ClassCastException:android.widget.RelativeLayout无法强制转换为com.slidingmenu.lib.SlidingMenu - Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to com.slidingmenu.lib.SlidingMenu java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法强制转换为android.support.v7.widget.RecyclerView $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout - java.lang.ClassCastException: android.widget.RelativeLayout java.lang.ClassCastException: android.widget.EditText - java.lang.ClassCastException: android.widget.EditText
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM