0

i am facing some problems in creating an enquiry form in my android app. The logcat is showing ClassCast error. I am a beginner in android so I don't know how to deal with it. The error is on line 50.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enquire);
        et_citylist = (EditText)findViewById(R.id.enquire_edit_city);
        et_name = (EditText)findViewById(R.id.enquire_edit_name);
        et_adult = (EditText)findViewById(R.id.enquire_edit_adult);
        et_child = (EditText)findViewById(R.id.enquire_edit_child);
        et_mail = (EditText)findViewById(R.id.enquire_edit_email);
        et_phone = (EditText)findViewById(R.id.enquire_edit_phone);
        et_datearrive = (EditText)findViewById(R.id.enquire_edit_datearrival);
         //LINE 50 -
        et_datedepart = (EditText)findViewById(R.id.enquire_edit_datedeparture);

        btn = (Button)findViewById(R.id.btnemail);
        btn.setVisibility(View.GONE);
        btn.setOnClickListener(this);
        to = "divyangbhambhani@gmail.com";
        subject = "Tour Package Details Summary";
        msg = "";
        chk = (CheckBox)findViewById(R.id.chkenquire);
        chk.setOnCheckedChangeListener(this);

        //setting calender for datepicker
        Calendar c = Calendar.getInstance();
        year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH);
        day = c.get(Calendar.DAY_OF_MONTH);
        year1 = c.get(Calendar.YEAR);
        month1 = c.get(Calendar.MONTH);
        day1 = c.get(Calendar.DAY_OF_MONTH);

        //get Intents

        Bundle extras = getIntent().getExtras();
            if(extras!=null)
            {
              getChecked = extras.getStringArrayList("list1");
              getname = extras.getString("name");
              getadult = extras.getString("adults");
              getchild = extras.getString("child");
              getmail = extras.getString("email");
              getphone = extras.getString("phone");
              getdatedept = extras.getString("datedept");
              getdatearr = extras.getString("datearr");
              stringbuilder = new StringBuilder();
              for(String value:getChecked){
                  stringbuilder.append(value).append(" ");
              }
           }
        //adding citylist to edittext   
            et_citylist.setText(stringbuilder);
            et_name.setText(getname);
            et_adult.setText(getadult);
            et_child.setText(getchild);
            et_mail.setText(getmail);
            et_phone.setText(getmail);
            et_datearrive.setText(getdatearr);
            et_datedepart.setText(getdatedept);

    }

Logcat:

07-17 01:42:59.420: E/AndroidRuntime(31134): FATAL EXCEPTION: main
07-17 01:42:59.420: E/AndroidRuntime(31134): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.travelplanner/com.example.travelplanner.EnquireActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
07-17 01:42:59.420: E/AndroidRuntime(31134):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
07-17 01:42:59.420: E/AndroidRuntime(31134):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
07-17 01:42:59.420: E/AndroidRuntime(31134):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-17 01:42:59.420: E/AndroidRuntime(31134):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
07-17 01:42:59.420: E/AndroidRuntime(31134):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 01:42:59.420: E/AndroidRuntime(31134):    at android.os.Looper.loop(Looper.java:137)
07-17 01:42:59.420: E/AndroidRuntime(31134):    at android.app.ActivityThread.main(ActivityThread.java:4448)
07-17 01:42:59.420: E/AndroidRuntime(31134):    at java.lang.reflect.Method.invokeNative(Native Method)
07-17 01:42:59.420: E/AndroidRuntime(31134):    at java.lang.reflect.Method.invoke(Method.java:511)
07-17 01:42:59.420: E/AndroidRuntime(31134):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
07-17 01:42:59.420: E/AndroidRuntime(31134):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
07-17 01:42:59.420: E/AndroidRuntime(31134):    at dalvik.system.NativeStart.main(Native Method)
07-17 01:42:59.420: E/AndroidRuntime(31134): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
07-17 01:42:59.420: E/AndroidRuntime(31134):    at com.example.travelplanner.EnquireActivity.onCreate(EnquireActivity.java:50)
07-17 01:42:59.420: E/AndroidRuntime(31134):    at android.app.Activity.performCreate(Activity.java:4465)
07-17 01:42:59.420: E/AndroidRuntime(31134):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)

REVISED : This is my XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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=".EnquireActivity" >

    <TextView
        android:id="@+id/enquire_txt_city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="City Name/s" />

    <EditText
        android:id="@+id/enquire_edit_city"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/enquire_txt_city"
        android:ems="10"
        android:singleLine="false"
        android:inputType="textPersonName" >
    </EditText>

    <TextView
        android:id="@+id/enquire_txt_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/enquire_edit_city"
        android:layout_marginTop="16dp"
        android:text="Name" />


    <EditText
        android:id="@+id/enquire_edit_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/enquire_txt_name"
        android:layout_alignBottom="@+id/enquire_txt_name"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/enquire_txt_name"
        android:ems="10"
        android:inputType="textPersonName" />

        <EditText
        android:id="@+id/enquire_edit_adult"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/enquire_txt_adult"
        android:layout_alignBottom="@+id/enquire_txt_adult"
        android:layout_alignLeft="@+id/enquire_edit_name"
        android:ems="10"
        android:inputType="number" />

    <EditText
        android:id="@+id/enquire_edit_child"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/enquire_txt_child"
        android:layout_alignBottom="@+id/enquire_txt_child"
        android:layout_alignRight="@+id/enquire_edit_name"
        android:ems="10"
        android:inputType="number" />

    <TextView
        android:id="@+id/enquire_txt_adult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/enquire_txt_name"
        android:layout_below="@+id/enquire_edit_name"
        android:layout_marginTop="20dp"
        android:text="Adults" />

        <TextView
        android:id="@+id/enquire_txt_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/enquire_txt_email"
        android:layout_marginRight="14dp"
        android:layout_toLeftOf="@+id/enquire_edit_child"
        android:text="Child" />

    <TextView
        android:id="@+id/enquire_txt_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/enquire_txt_adult"
        android:layout_below="@+id/enquire_edit_adult"
        android:layout_marginTop="22dp"
        android:text="Email Address" />

    <EditText
        android:id="@+id/enquire_edit_email"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/enquire_txt_email"
        android:layout_below="@+id/enquire_txt_email"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <TextView
        android:id="@+id/enquire_txt_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/enquire_edit_email"
        android:layout_centerVertical="true"
        android:text="Contact No." />

    <EditText
        android:id="@+id/enquire_edit_phone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/enquire_txt_phone"
        android:ems="10"
        android:inputType="phone" />

    <TextView
        android:i1d="@+id/enquire_txt_datedeparture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/enquire_edit_phone"
        android:layout_below="@+id/enquire_edit_phone"
        android:text="Departure" />

    <EditText
        android:id="@+id/enquire_edit_datedeparture"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/chkenquire"
        android:layout_alignLeft="@+id/chkenquire"
        android:ems="10"
        android:inputType="date" >

    </EditText>

    <TextView
        android:id="@+id/enquire_txt_datearrival"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignLeft="@+id/enquire_txt_child"
        android:text="Arrival" />

    <EditText
        android:id="@+id/enquire_edit_datearrival"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView2"
        android:layout_below="@+id/textView2"
        android:ems="10"
        android:inputType="date" />

    <CheckBox
        android:id="@+id/chkenquire"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btnemail"
        android:layout_alignLeft="@+id/btnemail"
        android:text="I Confirm These Details Are Relevant As Per My Concern"
        android:textSize="11sp" />

    <Button
        android:id="@+id/btnemail"
        android:background="#46C9F4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:textColor="#ffffff"
        android:layout_marginBottom="32dp"
        android:text="Send Enquiry" />

</RelativeLayout>
divyang7
  • 309
  • 3
  • 10
  • 22

2 Answers2

4

This exception is coming when you give the id of TextView and cast it on EditText view.

Just check all your id are correct one and type of this id are EditText view .

et_datedepart = (EditText)findViewById(R.id.enquire_edit_datedeparture);

May be "R.id.enquire_edit_datedeparture" is a id of TextView & that is way it gives class cast exception.

et_datedperart will be a EditText right?

Megha
  • 1,581
  • 2
  • 18
  • 33
0

Check your activity_enquire.xml.

You are trying to assign the ID of TextView to EditText and that's why you are getting ClassCastException.

AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
  • On a different note, please do not approve [suggested edits using backticks for non-code](http://stackoverflow.com/review/suggested-edits/2693060), see e.g. [here](http://meta.gaming.stackexchange.com/q/7437/88) why – Tobias Kienzler Aug 10 '13 at 06:24