gpt4 book ai didi

java.lang.ClassCastException : android. 部件.EditText

转载 作者:太空狗 更新时间:2023-10-29 15:00:48 25 4
gpt4 key购买 nike

这是我的 JAVA 代码。当我运行我的应用程序时,它崩溃了,我的日志中出现了以下错误

    11-21 00:21:48.828: E/AndroidRuntime(349): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.geekosoft.contactsonline/com.geekosoft.contactsonline.AddContact}: java.lang.ClassCastException: android.widget.EditText    11-21 00:21:48.828: E/AndroidRuntime(349):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)    11-21 00:21:48.828: E/AndroidRuntime(349):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)    11-21 00:21:48.828: E/AndroidRuntime(349):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)    11-21 00:21:48.828: E/AndroidRuntime(349):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)    11-21 00:21:48.828: E/AndroidRuntime(349):  at android.os.Handler.dispatchMessage(Handler.java:99)    11-21 00:21:48.828: E/AndroidRuntime(349):  at android.os.Looper.loop(Looper.java:123)    11-21 00:21:48.828: E/AndroidRuntime(349):  at android.app.ActivityThread.main(ActivityThread.java:3683)    11-21 00:21:48.828: E/AndroidRuntime(349):  at java.lang.reflect.Method.invokeNative(Native Method)    11-21 00:21:48.828: E/AndroidRuntime(349):  at java.lang.reflect.Method.invoke(Method.java:507)    11-21 00:21:48.828: E/AndroidRuntime(349):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)    11-21 00:21:48.828: E/AndroidRuntime(349):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)    11-21 00:21:48.828: E/AndroidRuntime(349):  at dalvik.system.NativeStart.main(Native Method)    11-21 00:21:48.828: E/AndroidRuntime(349): Caused by: java.lang.ClassCastException: android.widget.EditText    11-21 00:21:48.828: E/AndroidRuntime(349):  at com.geekosoft.contactsonline.AddContact.initilizeEditContactInfo(AddContact.java:123)    11-21 00:21:48.828: E/AndroidRuntime(349):  at com.geekosoft.contactsonline.AddContact.initilize(AddContact.java:68)    11-21 00:21:48.828: E/AndroidRuntime(349):  at com.geekosoft.contactsonline.AddContact.onCreate(AddContact.java:42)    11-21 00:21:48.828: E/AndroidRuntime(349):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)    11-21 00:21:48.828: E/AndroidRuntime(349):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

and here is my Java code

    package com.geekosoft.contactsonline;    import java.util.ArrayList;    import java.util.Iterator;    import android.app.Activity;    import android.content.ContentResolver;    import android.content.Context;    import android.database.Cursor;    import android.os.Build;    import android.os.Bundle;    import android.provider.ContactsContract;    import android.provider.ContactsContract.CommonDataKinds.Phone;    import android.util.Log;    import android.view.LayoutInflater;    import android.view.View;    import android.view.View.OnClickListener;    import android.widget.ArrayAdapter;    import android.widget.Button;    import android.widget.EditText;    import android.widget.Spinner;    import android.widget.TableLayout;    public class AddContact extends Activity implements OnClickListener {        public static final String TAG = AddContact.class.getSimpleName();        ArrayList contactPhoneTypes, contactEmailTypes, contactIMTypes, contactAddressTypes;        String contactid;        int phoneNumbersIdStarter, emailIdStarter, imIdStarter, addressIdStarter;        LayoutInflater layoutInflater;        EditText name, number;        Spinner phoneType, emailType, addressType;        Button addPhoneField, addEmailField, addIMField, addAddressField;        TableLayout phoneFiledsCont, emailFiledsCont, imFiledsCont, addressFiledsCont;        @Override        protected void onCreate (Bundle contactsOnline) {            super.onCreate(contactsOnline);            setContentView(R.layout.add_contact);            initilize();        }        public void initilize () {            initilizeSpinnerTypes();            phoneNumbersIdStarter = 1000;            emailIdStarter = 2000;            imIdStarter = 3000;            addressIdStarter = 4000;            layoutInflater =  (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);            name = (EditText) findViewById(R.id.etContactName);            addPhoneField = (Button) findViewById(R.id.bAddPhoneField);            addEmailField = (Button) findViewById(R.id.bAddEmailField);            addIMField = (Button) findViewById(R.id.bAddIMField);            addAddressField = (Button) findViewById(R.id.bAddaddressField);            phoneFiledsCont = (TableLayout) findViewById(R.id.tlPhoneFiledCont);            emailFiledsCont = (TableLayout) findViewById(R.id.tlEmailFiledsCont);            imFiledsCont = (TableLayout) findViewById(R.id.tlIMFiledsCont);            addressFiledsCont = (TableLayout) findViewById(R.id.tlAddressFiledsCont);            addPhoneField.setOnClickListener(this);            addEmailField.setOnClickListener(this);            addIMField.setOnClickListener(this);            addAddressField.setOnClickListener(this);            initilizeEditContactInfo();        }        public void initilizeSpinnerTypes() {            contactPhoneTypes = new ArrayList();            contactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_HOME);            contactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_WORK);            contactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);            contactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_OTHER);    //      contactPhoneTypes.add(ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM);            contactEmailTypes = new ArrayList();            contactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_HOME);            contactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_WORK);            contactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_MOBILE);            contactEmailTypes.add(ContactsContract.CommonDataKinds.Email.TYPE_OTHER);            contactIMTypes = new ArrayList();            contactIMTypes.add(ContactsContract.CommonDataKinds.Im.TYPE_HOME);            contactIMTypes.add(ContactsContract.CommonDataKinds.Im.TYPE_WORK);            contactIMTypes.add(ContactsContract.CommonDataKinds.Im.TYPE_OTHER);            contactAddressTypes = new ArrayList();            contactAddressTypes.add(ContactsContract.CommonDataKinds.SipAddress.TYPE_HOME);            contactAddressTypes.add(ContactsContract.CommonDataKinds.SipAddress.TYPE_WORK);            contactAddressTypes.add(ContactsContract.CommonDataKinds.SipAddress.TYPE_OTHER);        }        public void initilizeEditContactInfo () {            contactid = getIntent().getStringExtra("contactId");            Log.d(TAG, "id = "+contactid);            Log.d(TAG, "query = "+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);            ContentResolver cr = getContentResolver();            Cursor contact = cr.query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone._ID+" = ?",new String[]{contactid}, "DISPLAY_NAME ASC");    //      Cursor contacts = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);            if (contact.getCount() > 0) {                contact.moveToFirst();                String name = contact.getString(contact.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));                Log.d(TAG, "name = "+name);                this.name.setText(name);                int loopCounter = 0;                if (Integer.parseInt(contact.getString(contact.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {                    Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = ?",new String[]{contactid}, null);                    while (phones.moveToNext()) {                        loopCounter++;                        int phoneType = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));                        String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                        Log.d(TAG, "phone = "+phoneNumber);                        addTextField("phone");                        EditText phoneNumberField = (EditText) findViewById(8084+phoneNumbersIdStarter);                        phoneNumberField.setText(phoneNumber);                        Log.d("Spinner Getting", "Spinner Id = " + (8083+phoneNumbersIdStarter));                        Spinner phoneNumberSpinner = (Spinner) findViewById(8083+phoneNumbersIdStarter);                        Log.d("After Spinner", "phone counter = " + phoneNumbersIdStarter);                        switch (phoneType) {                            case Phone.TYPE_MOBILE:    //                          phoneNumberSpinner.setSelection(2, false);                                break;                            case Phone.TYPE_HOME:    //                          phoneNumberSpinner.setSelection(0, false);                                String phoneTypesCodes = "home = "+Phone.TYPE_HOME+"mobile = "+Phone.TYPE_MOBILE+"work = "+Phone.TYPE_WORK                                        +"other = "+Phone.TYPE_OTHER;                                Log.d(name + "(home number)", phoneNumber + phoneTypesCodes);                                break;                            case Phone.TYPE_WORK:    //                          phoneNumberSpinner.setSelection(1, false);                                break;                            case Phone.TYPE_OTHER:    //                          phoneNumberSpinner.setSelection(3, false);                                break;                        }                    }                     phones.close();                }            }            contact.close();        }        @Override        public void onClick(View v) {            switch (v.getId()) {            case R.id.bAddPhoneField:                addTextField("phone");                break;            case R.id.bAddEmailField:                addTextField("email");                break;            case R.id.bAddIMField:                addTextField("im");                break;            case R.id.bAddaddressField:                addTextField("address");                break;            }        }        public void addTextField(String fieldType) {            // for text field code is 84            // for spinner code is 83            if (fieldType.equals("phone")) {                // for phone number code is 80                phoneNumbersIdStarter++;                View view = layoutInflater.inflate(R.layout.add_phone_row, null);                EditText phoneNumberField = (EditText) view.findViewById(R.id.etContactPhone);                Spinner phoneTypeSpinner = (Spinner) view.findViewById(R.id.sContactPhoneType);                Log.d(TAG, "spinner id = " + phoneTypeSpinner.getId());                phoneNumberField.setId(8084+phoneNumbersIdStarter);                phoneTypeSpinner.setId(8083+phoneNumbersIdStarter);                Log.d("Spinner Created", "Spinner Id = " + phoneTypeSpinner.getId());                setSpinnerValues(phoneTypeSpinner, "phone");                phoneFiledsCont.addView(view);            } else if (fieldType.equals("email")) {                // for email code is 69                emailIdStarter++;                View view = layoutInflater.inflate(R.layout.add_email_row, null);                EditText emailAddressField = (EditText) view.findViewById(R.id.etContactEmail);                Spinner emailTypeSpinner = (Spinner) view.findViewById(R.id.sContactEmailType);                emailAddressField.setId(6984+emailIdStarter);                emailTypeSpinner.setId(6983+emailIdStarter);                Log.d(TAG, "inside true");                setSpinnerValues(emailTypeSpinner, "email");                Log.d(TAG, "inside true 2");                emailFiledsCont.addView(view);            } else if (fieldType.equals("im")) {                // for address code is 65                imIdStarter++;                View view = layoutInflater.inflate(R.layout.add_im_row, null);                EditText imField = (EditText) view.findViewById(R.id.etContactIM);                Spinner imTypeSpinner = (Spinner) view.findViewById(R.id.sContactIMType);                imField.setId(6584+imIdStarter);                imTypeSpinner.setId(6583+imIdStarter);                setSpinnerValues(imTypeSpinner, "im");                imFiledsCont.addView(view);            } else if (fieldType.equals("address")) {                // for im code is 73                addressIdStarter++;                View view = layoutInflater.inflate(R.layout.add_address_row, null);                EditText addressField = (EditText) view.findViewById(R.id.etContactAddress);                Spinner addressTypeSpinner = (Spinner) view.findViewById(R.id.sContactAddressType);                addressField.setId(7384+addressIdStarter);                addressTypeSpinner.setId(7383+addressIdStarter);                setSpinnerValues(addressTypeSpinner, "address");                addressFiledsCont.addView(view);            }        }        public void setSpinnerValues (Spinner spinner, String spinnerType) {            ArrayAdapter spinnerAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);            spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);            Iterator iter = null;            if (spinnerType.equals("phone")) {                iter = contactPhoneTypes.iterator();            } else if (spinnerType.equals("email")) {                iter = contactEmailTypes.iterator();            } else if (spinnerType.equals("im")) {                iter = contactIMTypes.iterator();            } else if (spinnerType.equals("address")) {                iter = contactAddressTypes.iterator();            }            while (iter.hasNext()) {                if (spinnerType.equals("phone")) {                    spinnerAdapter.add(ContactsContract.CommonDataKinds.Phone.getTypeLabel(this.getResources(), iter.next(), getString(R.string.undefinedTypeLabel)).toString());                } else if (spinnerType.equals("email")) {                    spinnerAdapter.add(ContactsContract.CommonDataKinds.Email.getTypeLabel(this.getResources(), iter.next(), getString(R.string.undefinedTypeLabel)).toString());                } else if (spinnerType.equals("im")) {                    spinnerAdapter.add(ContactsContract.CommonDataKinds.Im.getTypeLabel(this.getResources(), iter.next(), getString(R.string.undefinedTypeLabel)).toString());                } else if (spinnerType.equals("address")) {                    spinnerAdapter.add(ContactsContract.CommonDataKinds.Im.getTypeLabel(this.getResources(), iter.next(), getString(R.string.undefinedTypeLabel)).toString());                }            }            spinner.setAdapter(spinnerAdapter);            spinner.setPrompt(getString(R.string.selectLabel));        }    }

here is my add_contact.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">

<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">

<TableLayout
android:layout_width="fill_parent" android:layout_height="wrap_content">

<TableRow>

<TextView android:text="Target Account"
android:layout_width="wrap_content" android:layout_height="wrap_content" />

</TableRow>
<TableRow>

<Spinner android:id="@+id/accountSpinner"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_weight="1" />

</TableRow>
<TableRow>

<TextView android:text="Contact Name"
android:layout_width="wrap_content" android:layout_height="wrap_content" />

</TableRow>
<TableRow>

<EditText android:id="@+id/etContactName"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_weight="1"/>

</TableRow>
<TableRow>

<TextView android:text="Contact Phone"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>

</TableRow>
<TableRow>

<Button android:id="@+id/bAddPhoneField"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Add Field"/>

</TableRow>

</TableLayout>
<TableLayout android:id="@+id/tlPhoneFiledCont"
android:layout_width="fill_parent" android:layout_height="wrap_content">

</TableLayout>
<TableLayout
android:layout_width="fill_parent" android:layout_height="wrap_content">

<TableRow>

<TextView android:text="Contact Email"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>

</TableRow>

</TableLayout>
<Button android:id="@+id/bAddEmailField"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Add Field"/>
<TableLayout android:id="@+id/tlEmailFiledsCont"
android:layout_width="fill_parent" android:layout_height="wrap_content">

</TableLayout>

<TextView android:text="Contact IM"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button android:id="@+id/bAddIMField"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Add Field"/>
<TableLayout android:id="@+id/tlIMFiledsCont"
android:layout_width="fill_parent" android:layout_height="wrap_content">

</TableLayout>

<TextView android:text="Contact Address"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button android:id="@+id/bAddaddressField"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Add Field"/>
<TableLayout android:id="@+id/tlAddressFiledsCont"
android:layout_width="fill_parent" android:layout_height="wrap_content">

</TableLayout>

<TableLayout
android:layout_width="fill_parent" android:layout_height="wrap_content">

<TableRow>

<Button android:text="@string/save"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:id="@+id/contactSaveButton" android:layout_weight="1"/>

</TableRow>

</TableLayout>

</LinearLayout>

</ScrollView>

这是我的 add_phone_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" >

<EditText android:id="@+id/etContactPhone"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:inputType="phone" />

<Spinner android:id="@+id/sContactPhoneType"
android:layout_width="wrap_content" android:layout_height="wrap_content" />

</TableRow>

我正在创建 SpinnerEditText 在下面的函数中动态

    if (fieldType.equals("phone")) {            // for phone number code is 80            phoneNumbersIdStarter++;            View view = layoutInflater.inflate(R.layout.add_phone_row, null);            EditText phoneNumberField = (EditText) view.findViewById(R.id.etContactPhone);            Spinner phoneTypeSpinner = (Spinner) view.findViewById(R.id.sContactPhoneType);            Log.d(TAG, "spinner id = " + phoneTypeSpinner.getId());            phoneNumberField.setId(8084+phoneNumbersIdStarter);            phoneTypeSpinner.setId(8083+phoneNumbersIdStarter);            Log.d("Spinner Created", "Spinner Id = " + phoneTypeSpinner.getId());            setSpinnerValues(phoneTypeSpinner, "phone");            phoneFiledsCont.addView(view);        }

并像下面的行一样获取它们的值

    EditText phoneNumberField = (EditText) findViewById(8084+phoneNumbersIdStarter);    Spinner phoneNumberSpinner = (Spinner) findViewById(8083+phoneNumbersIdStarter);

并且每次我添加这些字段时也会在 phoneNumbersIdStarter

中添加 1

如果我在我的代码中注释以下行,它运行良好

    Spinner phoneNumberSpinner = (Spinner) findViewById(8083+phoneNumbersIdStarter);

最佳答案

您是否有某些特定原因不使用 R.id. 访问小部件?

我不认为使用“8083+phoneNumbersIdStarter”是个好主意,因为在类 R 中生成的 int id 会在每次编译时发生变化,所以 int 8083+phoneNumbersIdStarter 可能指的是一个不是的小部件一个 EditText。

所以,改成这样:

Spinner phoneNumberSpinner = (Spinner) findViewById(R.id.your_spinner_id);

关于java.lang.ClassCastException : android. 部件.EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27048071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com