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

13,381

Solution 1

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.

Solution 2

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

Share:
13,381

Related videos on Youtube

barca_d
Author by

barca_d

Updated on September 15, 2022

Comments

  • barca_d
    barca_d over 1 year

    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
    }