我正在从设备电话簿中获取联系人姓名,联系电话,联系电子邮件。直到现在我正在获取联系人姓名和号码,它工作正常。现在我试图添加电子邮件到名称和号码。但我得到非法论证异常。请让我知道我在这里做错了什么。以下是我的询问:
cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
String[] from = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, String.valueOf(ContactsContract.CommonDataKinds.Email.CONTENT_URI), ContactsContract.CommonDataKinds.Phone._ID};
int[] to = {R.id.textViewName, R.id.textViewNumber, R.id.textViewEmail};
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.custom_spinner_row, cursor1, from, to, 0);
我通过旋转器来填充这三个细节:
contacts1 = (Spinner) findViewById(R.id.spinnerContacts);
contacts1.setAdapter(myCursorAdapter);
contacts1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "The value of Position selected is " + position, Toast.LENGTH_LONG).show();
Cursor selectedCursor = (Cursor) parent.getItemAtPosition(position);
contactNumber = selectedCursor.getString(selectedCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactName = selectedCursor.getString(selectedCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
contactEmail = selectedCursor.getString(selectedCursor.getColumnIndex(String.valueOf(ContactsContract.CommonDataKinds.Email.CONTENT_URI)));
Log.d("Pana", "The value of strNumber is " + contactNumber);
Log.d("Pana", "The value of strName is " + contactName);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
我的数据库架构:
public static final String DATABASE_NAME = "task_management_database";
public static final String TABLE_NAME = "DataTable";
public static final String UID = "_id";
public static final String TABLE_NAME_CHILD = "ChildDataTable";
public static final String UIDCHILD = "_idchild";
public static final String TASK_NAME = "Task_name";
public static final String SUB_TASK_NAME = "Sub_Task_name";
public static final String CONTACT_NAME = "Contact_Name";
public static final String CONTACT_NUMBER = "Contact_Number";
public static final String CONTACT_EMAIL = "Contact_Email";
public static final String DESCRIPTION = "Description";
public static final String REMARKS = "Remarks";
public static final String DATE = "Date";
public static final String TIME = "Time";
public static final String ESTIMATED_COMPLETION_DATE = "EstCompDate";
public static final String ESTIMATED_COMPLETION_TIME = "EstCompTime";
public static final String ACTUAL_COMPLETION_DATE = "ActCompDate";
public static final String ACTUAL_COMPLETION_TIME = "ActCompTime";
public static final String NOTIFY_DATE = "NotifyDate";
public static final String NOTIFY_TIME = "NotifyTime";
public static final int DATABASE_VERSION = 1;
public static final String SUB_TASK_NUMBER = "sub_task_number";
public static final String CREATE_TABLE = " CREATE TABLE " + TABLE_NAME +
"(" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TASK_NAME + " VARCHAR(250)," + CONTACT_NAME + " VARCHAR(250)," + CONTACT_NUMBER + " VARCHAR(250),"
+ CONTACT_EMAIL + " VARCHAR(250)," + DESCRIPTION + " VARCHAR(250), " + REMARKS + " VARCHAR(250),"
+ DATE + " VARCHAR(250)," + TIME + " VARCHAR(250)," + ESTIMATED_COMPLETION_DATE + " VARCHAR(250), " + ESTIMATED_COMPLETION_TIME + " VARCHAR(250), "
+ ACTUAL_COMPLETION_DATE + " VARCHAR(250), " + ACTUAL_COMPLETION_TIME + " VARCHAR(250), " + NOTIFY_DATE + " VARCHAR(250), " + NOTIFY_TIME + " VARCHAR(250) );";
我的Adb追踪:
03-06 12:06:24.369 687-702/android.process.acore E/SQLiteLog﹕ (284) automatic index on sqlite_sq_A7126FB0(STAT_DATA_ID)
03-06 12:06:24.467 1323-1323/com.ms.t.tms E/Cursor﹕ requesting column name with table name -- content://com.android.contacts/data/emails
java.lang.Exception
at android.database.AbstractCursor.getColumnIndex(AbstractCursor.java:279)
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:301)
at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:78)
at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:333)
at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:107)
at com.ms.t.tms.InsertActivity.onCreate(InsertActivity.java:74)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-06 12:06:24.502 1323-1323/com.ms.t.tms D/AndroidRuntime﹕ Shutting down VM
03-06 12:06:24.517 1323-1323/com.ms.t.tms E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ms.t.tms, PID: 1323
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ms.t.tms/com.ms.t.tms.InsertActivity}: java.lang.IllegalArgumentException: column 'content://com.android.contacts/data/emails' does not exist
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.IllegalArgumentException: column 'content://com.android.contacts/data/emails' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:78)
at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:333)
at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:107)
at com.ms.t.tms.InsertActivity.onCreate(InsertActivity.java:74)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
错误指向myCursorAdapter行。我觉得错误是在cursor1行。但我不确定。请帮我解决这个问题。欢迎所有建议。提前谢谢。