编程语言
首页 > 编程语言> > Android java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.EditText

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

作者:互联网

单击按钮将我从一个活动带到另一个活动时,我在Android中收到以下异常(我是Android开发中的新手,因此可能不是最聪明的问题):

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.EditText

我试过几次清理项目,尝试过Android Tools中的Fix Project Properties选项,已经检查了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" />

这是我在活动中引发异常的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
}

解决方法:

您的代码说:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/editCustomerPhone"

editCustomerPhone是相对布局.

标签:classcastexception,android,java,android-layout
来源: https://codeday.me/bug/20191012/1899247.html