0
public class ImportContactsActivity extends Activity {
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact);
        TextView txt = (TextView)findViewById(R.id.con);
        String msg = "";

        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {
                String name = cursor.getString(cursor.getColumnIndexOrThrow(People.NAME));
                String number = cursor.getString(cursor.getColumnIndexOrThrow(People.NUMBER));
                String email = cursor.getString(cursor.getColumnIndexOrThrow(People.PRIMARY_EMAIL_ID));

                msg += name + " " + number + " " + email + "\n";
        }
        txt.setText(msg.toString());
    }
}

バックアップの代替手段として、現在のすべての電話連絡先(名前+電話番号)を含む電子メールをアプリケーションが送信できるアプリケーションを開発しています。電話の連絡先情報をすべて抽出してTextViewに表示する方法を試していますが、できません。誰かに相談してください。ありがとう...

LogCat
10-09 15:22:48.634: E/AndroidRuntime(680): FATAL EXCEPTION: main
10-09 15:22:48.634: E/AndroidRuntime(680): java.lang.RuntimeException: Unable to start   activity ComponentInfo{com.example.importcontacts/com.example.importcontacts.ImportContactsActivity} : java.lang.IllegalArgumentException: column 'name' does not exist
10-09 15:22:48.634: E/AndroidRuntime(680):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-09 15:22:48.634: E/AndroidRuntime(680):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-09 15:22:48.634: E/AndroidRuntime(680):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-09 15:22:48.634: E/AndroidRuntime(680):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-09 15:22:48.634: E/AndroidRuntime(680):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 15:22:48.634: E/AndroidRuntime(680):  at android.os.Looper.loop(Looper.java:137)
10-09 15:22:48.634: E/AndroidRuntime(680):  at android.app.ActivityThread.main(ActivityThread.java:4424)
10-09 15:22:48.634: E/AndroidRuntime(680):  at java.lang.reflect.Method.invokeNative(Native Method)
10-09 15:22:48.634: E/AndroidRuntime(680):  at java.lang.reflect.Method.invoke(Method.java:511)
10-09 15:22:48.634: E/AndroidRuntime(680):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-09 15:22:48.634: E/AndroidRuntime(680):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-09 15:22:48.634: E/AndroidRuntime(680):  at dalvik.system.NativeStart.main(Native Method)
10-09 15:22:48.634: E/AndroidRuntime(680): Caused by: java.lang.IllegalArgumentException: column 'name' does not exist
10-09 15:22:48.634: E/AndroidRuntime(680):  at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:267)
10-09 15:22:48.634: E/AndroidRuntime(680):  at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:78)
10-09 15:22:48.634: E/AndroidRuntime(680):  at com.example.importcontacts.ImportContactsActivity.onCreate(ImportContactsActivity.java:46)
10-09 15:22:48.634: E/AndroidRuntime(680):  at android.app.Activity.performCreate(Activity.java:4465)
10-09 15:22:48.634: E/AndroidRuntime(680):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-09 15:22:48.634: E/AndroidRuntime(680):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-09 15:22:48.634: E/AndroidRuntime(680):  ... 11 more
4

1 に答える 1

2

ContactsContract.CommonDataKinds.Phone.CONTENT_URI代わりに使用ContactsContract.Contacts.CONTENT_URIして、電話番号を取得し、これらの回線を変更できるようにします

String name = cursor.getString(cursor.getColumnIndexOrThrow(People.NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(People.NUMBER));
String email = cursor.getString(cursor.getColumnIndexOrThrow(People.PRIMARY_EMAIL_ID));

String name = cursor.getString(cursor.getColumnIndexOrThrow(Phone.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndexOrThrow(Phone.NUMBER));

email_idを取得するには、別のクエリが必要になります。最初のカーソルから、次のような連絡先IDを取得できます。

long contactId = cursor.getLong(cursor.getColumnIndexOrThrow(Phone.CONTACT_ID));

contactId次に、フィルターでを使用して電子メールURIを照会します

long emailId;
Cursor cur = getContentResolver()
                    .query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                     new String[]{"_id"},
                     "contact_id = " + contactId,
                     null,null);
if(cur != null && cur.moveToFirst()){
    emailId = cur.getLong(0);
}
于 2012-10-09T16:17:38.407 に答える