【发布时间】:2015-01-18 19:32:50
【问题描述】:
这是我的 JAVA 代码。当我运行我的应用程序时,它会崩溃,并且我的日志中出现以下错误
11-21 00:21:48.828: E/AndroidRuntime(349): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.geekosoft.contactsonline/com.geekosoft.contactsonline.AddContact}: java.lang.ClassCastException: android .widget.EditText 11-21 00:21:48.828: E/AndroidRuntime(349): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 11-21 00:21:48.828: E/AndroidRuntime(349): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 11-21 00:21:48.828: E/AndroidRuntime(349): 在 android.app.ActivityThread.access$1500(ActivityThread.java:117) 11-21 00:21:48.828: E/AndroidRuntime(349): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 11-21 00:21:48.828: E/AndroidRuntime(349): 在 android.os.Handler.dispatchMessage(Handler.java:99) 11-21 00:21:48.828: E/AndroidRuntime(349): 在 android.os.Looper.loop(Looper.java:123) 11-21 00:21:48.828: E/AndroidRuntime(349): 在 android.app.ActivityThread.main(ActivityThread.java:3683) 11-21 00:21:48.828: E/AndroidRuntime(349): 在 java.lang.reflect.Method.invokeNative(Native Method) 11-21 00:21:48.828: E/AndroidRuntime(349): 在 java.lang.reflect.Method.invoke(Method.java:507) 11-21 00:21:48.828: E/AndroidRuntime(349): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 11-21 00:21:48.828: E/AndroidRuntime(349): 在 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): 由: java.lang.ClassCastException: android.widget.EditText 11-21 00:21:48.828: E/AndroidRuntime(349): 在 com.geekosoft.contactsonline.AddContact.initilizeEditContactInfo(AddContact.java:123) 11-21 00:21:48.828: E/AndroidRuntime(349): 在 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): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 11-21 00:21:48.828: E/AndroidRuntime(349): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)这是我的 Java 代码
包 com.geekosoft.contactsonline; 导入 java.util.ArrayList; 导入 java.util.Iterator; 导入android.app.Activity; 导入 android.content.ContentResolver; 导入android.content.Context; 导入android.database.Cursor; 导入android.os.Build; 导入android.os.Bundle; 导入 android.provider.ContactsContract; 导入 android.provider.ContactsContract.CommonDataKinds.Phone; 导入android.util.Log; 导入 android.view.LayoutInflater; 导入android.view.View; 导入 android.view.View.OnClickListener; 导入 android.widget.ArrayAdapter; 导入android.widget.Button; 导入 android.widget.EditText; 导入 android.widget.Spinner; 导入 android.widget.TableLayout; 公共类 AddContact 扩展 Activity 实现 OnClickListener { public static final String TAG = AddContact.class.getSimpleName(); ArrayList contactPhoneTypes、contactEmailTypes、contactIMTypes、contactAddressTypes; 字符串联系方式; int phoneNumbersIdStarter, emailIdStarter, imIdStarter, addressIdStarter; 布局充气机布局充气机; EditText 名称、编号; 微调器电话类型、电子邮件类型、地址类型; 按钮 addPhoneField、addEmailField、addIMField、addAddressField; TableLayout phoneFiledsCont、emailFiledsCont、imFiledsCont、addressFiledsCont; @覆盖 protected void onCreate (Bundle contactsOnline) { super.onCreate(contactsOnline); setContentView(R.layout.add_contact); 初始化(); } 公共无效初始化(){ initilizeSpinnerTypes(); phoneNumbersIdStarter = 1000; emailIdStarter = 2000; imIdStarter = 3000; 地址IdStarter = 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(); } 公共无效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); } 公共无效initilizeEditContactInfo(){ contactid = getIntent().getStringExtra("contactId"); Log.d(TAG, "id = "+contactid); Log.d(TAG, "query = "+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); 内容解析器 cr = getContentResolver(); 光标联系人 = cr.query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone._ID+" = ?",new String[]{contactid}, "DISPLAY_NAME ASC"); // 光标联系人 = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); if (contact.getCount() > 0) { 联系人.moveToFirst(); 字符串名称 = contact.getString(contact.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); Log.d(TAG, "name = "+name); this.name.setText(name); int 循环计数器 = 0; if (Integer.parseInt(contact.getString(contact.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 光标电话 = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = ?",new String[]{contactid}, null); 而(phones.moveToNext()){ 循环计数器++; int phoneType = phone.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); String phoneNumber = phone.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); Log.d(TAG, "phone = "+phoneNumber); addTextField("电话"); EditText phoneNumberField = (EditText) findViewById(8084+phoneNumbersIdStarter); phoneNumberField.setText(phoneNumber); Log.d("Spinner 获取", "Spinner Id = " + (8083+phoneNumbersIdStarter)); 微调器 phoneNumberSpinner = (Spinner) findViewById(8083+phoneNumbersIdStarter); Log.d("在 Spinner 之后", "phone counter = " + phoneNumbersIdStarter); 开关(电话类型){ 案例电话.TYPE_MOBILE: // phoneNumberSpinner.setSelection(2, false); 休息; 案例电话.TYPE_HOME: // phoneNumberSpinner.setSelection(0, false); 字符串 phoneTypesCodes = "home = "+Phone.TYPE_HOME+"mobile = "+Phone.TYPE_MOBILE+"work = "+Phone.TYPE_WORK +"其他 = "+电话.TYPE_OTHER; Log.d(name + "(home number)", phoneNumber + phoneTypesCodes); 休息; 案例电话.TYPE_WORK: // phoneNumberSpinner.setSelection(1, false); 休息; 案例电话.TYPE_OTHER: // phoneNumberSpinner.setSelection(3, false); 休息; } } 电话.close(); } } 联系。关闭(); } @覆盖 公共无效 onClick(查看 v){ 开关 (v.getId()) { 案例 R.id.bAddPhoneField: addTextField("电话"); 休息; 案例 R.id.bAddEmailField: addTextField("电子邮件"); 休息; 案例 R.id.bAddIMField: addTextField("im"); 休息; 案例 R.id.bAddaddressField: addTextField("地址"); 休息; } } 公共无效addTextField(字符串字段类型){ // 对于文本字段代码是 84 // 微调器代码为 83 if (fieldType.equals("phone")) { // 电话号码代码为 80 phoneNumbersIdStarter++; 查看视图 = 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, "电话"); phoneFiledsCont.addView(view); } else if (fieldType.equals("email")) { // 对于电子邮件代码是 69 emailIdStarter++; 查看视图 = layoutInflater.inflate(R.layout.add_email_row, null); EditText emailAddressField = (EditText) view.findViewById(R.id.etContactEmail); 微调器 emailTypeSpinner = (微调器) view.findViewById(R.id.sContactEmailType); emailAddressField.setId(6984+emailIdStarter); emailTypeSpinner.setId(6983+emailIdStarter); Log.d(TAG, "内部为真"); setSpinnerValues(emailTypeSpinner, "email"); Log.d(TAG, "inside true 2"); emailFiledsCont.addView(view); } else if (fieldType.equals("im")) { //地址码是65 imIdStarter++; 查看视图 = layoutInflater.inflate(R.layout.add_im_row, null); EditText imField = (EditText) view.findViewById(R.id.etContactIM); 微调器 imTypeSpinner = (微调器) view.findViewById(R.id.sContactIMType); imField.setId(6584+imIdStarter); imTypeSpinner.setId(6583+imIdStarter); setSpinnerValues(imTypeSpinner, "im"); imFiledsCont.addView(view); } else if (fieldType.equals("address")) { // 对于 im 代码是 73 地址IdStarter++; 查看视图 = 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, "地址"); addressFiledsCont.addView(view); } } 公共无效 setSpinnerValues(微调器微调器,字符串微调器类型){ ArrayAdapter spinnerAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item); spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 迭代器 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(); } 而(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)); } }这是我的 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>
我正在下面的函数中动态创建Spinner 和EditText
并像下面的行一样获取它们的值
EditText phoneNumberField = (EditText) findViewById(8084+phoneNumbersIdStarter); 微调器 phoneNumberSpinner = (Spinner) findViewById(8083+phoneNumbersIdStarter);每次我添加这些字段时,phoneNumbersIdStarter 中也会添加 1
如果我在我的代码中注释以下行,它运行得很好
微调器 phoneNumberSpinner = (Spinner) findViewById(8083+phoneNumbersIdStarter);【问题讨论】:
-
基本上,您正在尝试将
EditText转换为Spinner。也许您正在使用findViewById查找错误的视图 -
不,我不会将
EditText转换为Spinner。如果您检查我的代码,我有一个函数addTextField,并且每次调用该函数时我都会创建一个EditText和一个Spinner。但是当我尝试在其他函数中访问Spinner时出现此错误。 -
@PiyushGupta 我也添加了 xml 文件
-
为什么要实现
OnClickListener? -
@jyoon 因为我在点击按钮时创建了
EditText和Spinner
标签: java android eclipse android-intent spinner