0

我正在尝试编写一个小应用程序来显示联系人并将其添加到 android 电话簿。我以这种方式添加联系人:

ContentValues cv = new ContentValues();
EditText te1 = (EditText)findViewById(R.id.EditText1);
EditText te2 = (EditText)findViewById(R.id.EditText2);
cv.put(ContactsContract.Data.RAW_CONTACT_ID, 400);
cv.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
cv.put(ContactsContract.CommonDataKinds.Phone.NUMBER, te2.getText().toString());
cv.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
cv.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, te1.getText().toString());
ContentResolver contentResolver = getContentResolver();
contentResolver.insert(ContactsContract.Data.CONTENT_URI, cv);
setContentView(R.layout.main);

代码成功执行,但看起来不起作用。谁能指出我的错误?日志输出:

I/Database(14340): sqlite returned: error code = 1, msg = table data has no column named display_name
E/Database(14340): Error inserting display_name=jsmith data1=57457644 data4=44675475 data2=2 mimetype_id=5 raw_contact_id=400
E/Database(14340): android.database.sqlite.SQLiteException: table data has no column named display_name: , while compiling: INSERT INTO data(display_name, data1, data4, data2, mimetype_id, raw_contact_id) VALUES(?, ?, ?, ?, ?, ?);
E/Database(14340):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
E/Database(14340):  at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
E/Database(14340):  at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
E/Database(14340):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
E/Database(14340):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
E/Database(14340):  at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1157)
E/Database(14340):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1577)
E/Database(14340):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1434)
E/Database(14340):  at com.android.providers.contacts.ContactsProvider2$DataRowHandler.insert(ContactsProvider2.java:1032)
E/Database(14340):  at com.android.providers.contacts.ContactsProvider2$CommonDataRowHandler.insert(ContactsProvider2.java:1388)
E/Database(14340):  at com.android.providers.contacts.ContactsProvider2$PhoneDataRowHandler.insert(ContactsProvider2.java:1638)
E/Database(14340):  at com.android.providers.contacts.ContactsProvider2.insertData(ContactsProvider2.java:2649)
E/Database(14340):  at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:2478)
E/Database(14340):  at com.android.providers.contacts.SemcContactsProvider2.insertInTransaction(SemcContactsProvider2.java:851)
E/Database(14340):  at com.android.providers.contacts.SQLiteContentProvider.insert(SQLiteContentProvider.java:95)
E/Database(14340):  at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:2294)
E/Database(14340):  at com.android.providers.contacts.SemcContactsProvider2.insert(SemcContactsProvider2.java:778)
E/Database(14340):  at android.content.ContentProvider$Transport.insert(ContentProvider.java:198)
E/Database(14340):  at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:146)
E/Database(14340):  at android.os.Binder.execTransact(Binder.java:324)
E/Database(14340):  at dalvik.system.NativeStart.run(Native Method)
E/Database(14340): Error inserting min_match=4467547 normalized_number=44675475 data_id=-1 raw_contact_id=400 using INSERT OR REPLACE  INTO phone_lookup(min_match, normalized_number, data_id, raw_contact_id) VALUES(?, ?, ?, ?);
4

1 回答 1

0

您正在访问 EditText 没有在开头显示导致您出现问题所以以这些方式修改您的代码--->

setContentView(R.layout.main);// put in the beginning of code
ContentValues cv = new ContentValues();
EditText te1 = (EditText)findViewById(R.id.EditText1);
EditText te2 = (EditText)findViewById(R.id.EditText2);
cv.put(ContactsContract.Data.RAW_CONTACT_ID, 400);
cv.put(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
cv.put(ContactsContract.CommonDataKinds.Phone.NUMBER, te2.getText().toString());
cv.put(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
cv.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, te1.getText().toString());
ContentResolver contentResolver = getContentResolver();
contentResolver.insert(ContactsContract.Data.CONTENT_URI, cv);
于 2012-09-06T14:22:51.803 回答