打开电话联系人列表,然后选择单个联系人

时间:2013-12-11 05:30:51

标签: android

我正在使用此代码打开我的联系人列表,然后选择要在我的UI中显示的单个联系人。我可以打开我的联系人列表,但当我点击任何联系人时,程序崩溃了。

etphone1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            // Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            // intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            // startActivityForResult(intent, 1);

            Intent pb = new Intent(Intent.ACTION_PICK,
                    ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(pb, 1);

        }
    });

我的onActivityForResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            Uri contactData = data.getData();
            Cursor cursor = managedQuery(contactData, null, null, null,
                    null);
            cursor.moveToFirst();

            String number = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
            TextView tv;
            tv = (TextView) findViewById(R.id.tv);

            tv.setText(number);

        }
    }

}

logcat的:

12-11 11:05:48.969: E/AndroidRuntime(16292): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r10-272B2B434F414D37413143/10 flg=0x1 }} to activity {com.sriyank.locationmessenger/com.sriyank.locationmessenger.SecondPage}: java.lang.IllegalArgumentException: column 'data1' does not exist
12-11 11:05:48.969: E/AndroidRuntime(16292):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3209)
12-11 11:05:48.969: E/AndroidRuntime(16292):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3252)
12-11 11:05:48.969: E/AndroidRuntime(16292):    at android.app.ActivityThread.access$1200(ActivityThread.java:143)
12-11 11:05:48.969: E/AndroidRuntime(16292):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
12-11 11:05:48.969: E/AndroidRuntime(16292):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 11:05:48.969: E/AndroidRuntime(16292):    at android.os.Looper.loop(Looper.java:137)
12-11 11:05:48.969: E/AndroidRuntime(16292):    at android.app.ActivityThread.main(ActivityThread.java:4960)
12-11 11:05:48.969: E/AndroidRuntime(16292):    at java.lang.reflect.Method.invokeNative(Native Method)
12-11 11:05:48.969: E/AndroidRuntime(16292):    at java.lang.reflect.Method.invoke(Method.java:511)
12-11 11:05:48.969: E/AndroidRuntime(16292):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-11 11:05:48.969: E/AndroidRuntime(16292):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-11 11:05:48.969: E/AndroidRuntime(16292):    at dalvik.system.NativeStart.main(Native Method)
12-11 11:05:48.969: E/AndroidRuntime(16292): Caused by: java.lang.IllegalArgumentException: column 'data1' does not exist
12-11 11:05:48.969: E/AndroidRuntime(16292):    at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:302)
12-11 11:05:48.969: E/AndroidRuntime(16292):    at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:78)
12-11 11:05:48.969: E/AndroidRuntime(16292):    at com.sriyank.locationmessenger.SecondPage.onActivityResult(SecondPage.java:59)

2 个答案:

答案 0 :(得分:1)

请勿在{{1​​}}初始化您的观看次数,因为onActivityResult初始化可能会导致问题。在Textview中声明它。从以下方法中删除onCreate()初始化并在onCreate()中初始化并检查。

TextView

<强>编辑:

尝试使用以下代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            Uri contactData = data.getData();
            Cursor cursor = managedQuery(contactData, null, null, null,
                    null);
            cursor.moveToFirst();

            String number = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
            //TextView tv;
            //tv = (TextView) findViewById(R.id.tv); //remove this lines.

            tv.setText(number);

        }
    }

}

现在,只要用户选择了一个联系人(并且可能选择了多个电话号码中的一个),您就可以正常方式检索数据:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.contact_picker);

    // this opens the activity. note the  Intent.ACTION_GET_CONTENT
    // and the intent.setType
  etphone1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // user BoD suggests using Intent.ACTION_PICK instead of .ACTION_GET_CONTENT to avoid the chooser
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            // BoD con't: CONTENT_TYPE instead of CONTENT_ITEM_TYPE
            intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            startActivityForResult(intent, 1);                
        }
    });
}

答案 1 :(得分:1)

以下代码将3文本视图设置为联系人姓名和电话号码。 设置按钮单击侦听器 设置所提到的意图。

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
    switch (v.getId()) {
    case R.id.l1:

        startActivityForResult(intent, 1);

        break;
    case R.id.l2:

        startActivityForResult(intent, 2);

        break;
    case R.id.l3:

        startActivityForResult(intent, 3);

        break;

    }
}

这是onActivityresult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (data != null) {
        Uri uri = data.getData();

        if (uri != null) {
            Cursor c = null;
            try {
                c = getContentResolver()
                        .query(uri,
                                new String[] {
                                        ContactsContract.CommonDataKinds.Phone.NUMBER,
                                        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
                                null, null, null);

                if (c != null && c.moveToFirst()) {
                    String number = c.getString(0);
                    String Name = c.getString(1);

                    if (requestCode == 1) {

                        tvname1.setText(Name);
                        tvphone1.setText(number);

                    } else if (requestCode == 2) {
                        tvname2.setText(Name);
                        tvphone2.setText(number);

                    } else if (requestCode == 3) {
                        tvname3.setText(Name);
                        tvphone3.setText(number);

                    }

                }
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }

    }

}