活动中的ClassCastException

时间:2013-07-17 07:18:01

标签: android exception-handling classcastexception

我在Android应用程序中创建查询表单时遇到一些问题。 logcat显示ClassCast错误。我是android的初学者,所以我不知道如何处理它。 错误在第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)

已修订:这是我的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>

2 个答案:

答案 0 :(得分:4)

当您提供TextView的ID 并将其投放到EditText视图时,会出现异常。

只需检查您的所有ID 是否正确,并且此ID的类型为EditText视图。

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

可能R.id.enquire_edit_datedeparture”是 ID TextView&amp;这是它给出class cast exception.

的方式

et_datedperart 将是 EditText对吗?

答案 1 :(得分:0)

检查activity_enquire.xml

您试图将TextView的ID分配给EditText,这就是您获得ClassCastException的原因。