从联系人列表中获取号码时关闭力量

时间:2014-07-28 06:13:12

标签: android android-contacts

我制作了一个用户必须注册所有详细信息的应用程序。对于我使用过按钮的联系信息,单击时会打开联系人列表。当用户选择特定联系人时,所选联系人的号码应该是显示在编辑文本中。但每当我选择一个联系人时,我的应用程序崩溃

代码。

 private String strFirstName, strLastName, strEmail, strPassword, strConfirmPassword, strMobile, contactNumber;
    private Intent intent;
 case R.id.bt_contacts:
                intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent, 1);
                break;
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK) {
            Uri contactData = data.getData();
            Cursor c = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            if (c.moveToFirst()) {
                String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                String number = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                if (number.equalsIgnoreCase("1")) {
                    Cursor phones = getActivity().getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
                            null, null);
                    phones.moveToFirst();
                    contactNumber = phones.getString(phones.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    etMobile.setText(contactNumber);

                }
            }

            etMobile.setText("" + contactNumber);
        }
    }

logcat的

07-28 11:42:31.228  12387-12387/example.com.pocketdocs E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=android:fragment:0, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/1135r974-27272727272727.3789r975-27272727272727/974 }} to activity {example.com.pocketdocs/example.com.pocketdocs.MainActivity.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3162)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3205)
            at android.app.ActivityThread.access$1100(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
            at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
            at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
            at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
            at android.database.CursorWrapper.getString(CursorWrapper.java:114)
            at example.com.pocketdocs.SignUp.SignUpFragment.onActivityResult(SignUpFragment.java:201)
            at android.app.Activity.dispatchActivityResult(Activity.java:5196)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3158)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3205)
            at android.app.ActivityThread.access$1100(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

问题出在这里

Uri contactData = data.getData();
    Cursor c = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

而应该是

Uri contactData = data.getData();
        Cursor c = getActivity().getContentResolver().query(contactData, null, null, null, null);

答案 1 :(得分:0)

根据日志猫:

android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

您正在尝试访问不存在的内容。我的猜测是返回的Cursor没有指向任何东西。因此c.moveToFirst()不会起作用,因为没有第一个要指向的元素。您可以使用c.getCount();

来确认这一点

由于您使用ContactsContract.Contacts.CONTENT_URI请求数据,因此您还应在查询中使用该数据:

Cursor c = getActivity().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);