ClassCast异常 - 无法将TextView强制转换为EditText

时间:2013-07-20 07:17:09

标签: android exception classcastexception

我在EnquireActivity中遇到ClassCast Exception。根据logcat,我对EditText和TextView的id有歧义。但是我为它们提供了不同的ID。作为Android的新手,我无法找出它为什么会发生......任何帮助都将不胜感激。

这是我的XML文件activity_enquire.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:id="@+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/enquire_txt_datedeparture"
        android:layout_alignBottom="@+id/enquire_txt_datedeparture"
        android:layout_toLeftOf="@+id/enquire_edit_child"
        android:text="Arrival" />

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

    <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>

这是EnquireActivity.java

package com.example.travelplanner;

import java.util.ArrayList;
import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;

public class EnquireActivity extends Activity implements OnClickListener, OnCheckedChangeListener {

    //setting var for datepicker
    private int year,year1;
    private int month,month1;
    private int day,day1;
    String date1, date2;
    static final int DATE_DIALOG_ID = 0;
    static final int DATE2_DIALOG_ID = 1;

    Button btn;
    StringBuilder stringbuilder;
    String to, subject,msg;
    String getname,getadult, getchild, getmail, getphone, getdatedept, getdatearr;
    ArrayList<String> getChecked;
    CheckBox chk;
    EditText et_citylist, et_name, et_adult, et_child, et_mail, et_phone, et_datedepart, et_datearrive;
    @Override
    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);
           //line 49
        et_phone = (EditText)findViewById(R.id.enquire_edit_phone);
        et_datearrive = (EditText)findViewById(R.id.enquire_edit_datearrival);
        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);

    }

    @SuppressWarnings("deprecation")
    public void showdate(View v) {
        showDialog(DATE_DIALOG_ID);

    }

    @SuppressWarnings("deprecation")
    public void showdate1(View v) {
        showDialog(DATE2_DIALOG_ID);
    }

    public void updateDate() {
        final String[] MONTHS = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        String mon = MONTHS[month]; 
        et_datedepart.setText(date1);
        }

    public void updateDate1() {
        final String[] MONTHS = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        String mon = MONTHS[month]; 
        et_datearrive.setText(date2);
        }

    private DatePickerDialog.OnDateSetListener datedialog = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int yy, int mm,
                int dd) {
            // TODO Auto-generated method stub
            month = mm;
            day = dd;
            year = yy;
            date1 = day+"/"+month+"/"+year;
            updateDate();
        }
    };

private DatePickerDialog.OnDateSetListener datedialog2 = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int yy, int mm,
                int dd) {
            // TODO Auto-generated method stub
            month1 = mm;
            day1 = dd;
            year1 = yy;
            date2 = day1+"/"+month1+"/"+year1;
            updateDate1();
        }
    };

    protected Dialog onCreateDialog(int id) {
        //new DatePick
         switch (id) {  
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,
                        datedialog, year, month,day);

            case DATE2_DIALOG_ID:
                return new DatePickerDialog(this,
                        datedialog2, year, month,day);
         }
         return null;
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.enquire, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        //Send Mail
        Intent email = new Intent(Intent.ACTION_SEND);
          email.putExtra(Intent.EXTRA_EMAIL, new String[]{ to});
          //email.putExtra(Intent.EXTRA_CC, new String[]{ to});
          //email.putExtra(Intent.EXTRA_BCC, new String[]{to});
          email.putExtra(Intent.EXTRA_SUBJECT, subject);
          email.putExtra(Intent.EXTRA_TEXT, msg);

          //need this to prompts email client only
          email.setType("message/rfc822");

          startActivity(Intent.createChooser(email, "Choose an Email client :"));
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        if(chk.isChecked())
            btn.setVisibility(View.VISIBLE);
        else
            btn.setVisibility(View.GONE);

    }

}

Logcat输出:

07-20 12:27:39.078: E/AndroidRuntime(6059): FATAL EXCEPTION: main
07-20 12:27:39.078: E/AndroidRuntime(6059): 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-20 12:27:39.078: E/AndroidRuntime(6059):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
07-20 12:27:39.078: E/AndroidRuntime(6059):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
07-20 12:27:39.078: E/AndroidRuntime(6059):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-20 12:27:39.078: E/AndroidRuntime(6059):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
07-20 12:27:39.078: E/AndroidRuntime(6059):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 12:27:39.078: E/AndroidRuntime(6059):     at android.os.Looper.loop(Looper.java:137)
07-20 12:27:39.078: E/AndroidRuntime(6059):     at android.app.ActivityThread.main(ActivityThread.java:4448)
07-20 12:27:39.078: E/AndroidRuntime(6059):     at java.lang.reflect.Method.invokeNative(Native Method)
07-20 12:27:39.078: E/AndroidRuntime(6059):     at java.lang.reflect.Method.invoke(Method.java:511)
07-20 12:27:39.078: E/AndroidRuntime(6059):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
07-20 12:27:39.078: E/AndroidRuntime(6059):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
07-20 12:27:39.078: E/AndroidRuntime(6059):     at dalvik.system.NativeStart.main(Native Method)
07-20 12:27:39.078: E/AndroidRuntime(6059): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
07-20 12:27:39.078: E/AndroidRuntime(6059):     at com.example.travelplanner.EnquireActivity.onCreate(EnquireActivity.java:49)
07-20 12:27:39.078: E/AndroidRuntime(6059):     at android.app.Activity.performCreate(Activity.java:4465)
07-20 12:27:39.078: E/AndroidRuntime(6059):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-20 12:27:39.078: E/AndroidRuntime(6059):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
07-20 12:27:39.078: E/AndroidRuntime(6059):     ... 11 more

1 个答案:

答案 0 :(得分:3)

我找不到任何不匹配的引用。

import android.widget.TextView;

我认为您的活动中不需要上面的包名称只需删除它并重新启动eclipse。这可能会帮到你。