0

連絡先リストから電話番号を選択したいのですが、電話番号を選択するためにこのコードを書きますが、これは例外をスローします

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
            case CONTACT_PICKER_RESULT:
                Bundle extras = data.getExtras();
                Set<String> keys = extras.keySet();
                Iterator<String> iterate = keys.iterator();
                while (iterate.hasNext()) {
                    String key = iterate.next();
                    Log.v("R", key + "[" + extras.get(key) + "]");
                }
                Uri result = data.getData();
                Cursor c = getContentResolver().query(result,null,null,null,null);
                try {
                    c.moveToFirst();
                    String name = c
                            .getString(c
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    Log.i("name", name);
                    String contactId = c.getString(c
                            .getColumnIndex(ContactsContract.Contacts._ID));
                    Log.i("Contact Id : ", contactId);
                    Cursor phones = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = " + contactId, null, null);
                    final String[] contactNumbers = new String[phones
                            .getCount()];
                    int count = 0;
                    while (phones.moveToNext()) {
                        contactNumbers[count] = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        count++;
                    }
                    phones.close();

                    if (contactNumbers.length > 1 ) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        builder.setTitle("Call Number");
                        builder.setItems(contactNumbers, new OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                ContactActivity.roamNumber
                                        .setText(contactNumbers[which]);

                            }
                        });

                        builder.create().show();

                    } else {
                        ContactActivity.roamNumber
                        .setText(contactNumbers[0]);
                    }

                } finally {
                    c.close();
                }

                break;
            }

        } else {
            // gracefully handle failure
            Log.i("", "Warning: activity result not ok");
        }


    }

注:Facebookの連絡先を選択して番号スロー例外を与えると、私の電話はFacebookと同期します:

10-01 13:44:17.369: E/AndroidRuntime(9991): FATAL EXCEPTION: main
10-01 13:44:17.369: E/AndroidRuntime(9991): java.lang.RuntimeException: Failure delivering result ResultInfo{who=Contact, request=1001, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/543r3759-2D333B2D45513B2D373D41.1160i730738002/3838 flg=0x1 (has extras) }} to activity {com.xeno.callRoaming/com.xeno.callRoaming.XenoCallRoamingActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
10-01 13:44:17.369: E/AndroidRuntime(9991):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at android.os.Looper.loop(Looper.java:123)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at android.app.ActivityThread.main(ActivityThread.java:3687)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at java.lang.reflect.Method.invokeNative(Native Method)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at java.lang.reflect.Method.invoke(Method.java:507)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at dalvik.system.NativeStart.main(Native Method)
10-01 13:44:17.369: E/AndroidRuntime(9991): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
10-01 13:44:17.369: E/AndroidRuntime(9991):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at android.database.CursorWrapper.getString(CursorWrapper.java:135)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at com.xeno.callRoaming.ContactPickerActivity.onActivityResult(ContactPickerActivity.java:67)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at android.app.ActivityGroup.dispatchActivityResult(ActivityGroup.java:119)
10-01 13:44:17.369: E/AndroidRuntime(9991):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
10-01 13:44:17.369: E/AndroidRuntime(9991):     ... 11 more

何が問題ですか ?

4

1 に答える 1

0

私はこのコードを使用して問題を解決します:

Uri result = data.getData();

                Cursor c = getContentResolver().query(result, null, null, null,
                        null);
                Log.d("Row Count c", c.getCount() +"");
                Log.d("result", result +"");

                try {
                    c.moveToFirst();
                    String name = c
                            .getString(c
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    Log.i("name", name);

                    String contactId = c.getString(c
                            .getColumnIndex(ContactsContract.Contacts._ID));
                    Log.i("Contact Id : ", contactId);
                    Cursor phoness = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = " + contactId, null, null);
                    Cursor phones = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, 
                            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME  +
                            " like ('"+name+"')", null, null);


                    if (phones.getCount() == 0) {
                        AlertDialog.Builder numbersBuilder = new AlertDialog.Builder(this);
                        numbersBuilder.setMessage("No Number Found ..!")
                        .setCancelable(true);
                        AlertDialog alert = numbersBuilder.create();
                        alert.show();
                        ContactActivity.roamNumber
                        .setText("");
                        break;
                    }

                    final String[] contactNumbers = new String[phones
                            .getCount()];
                    int count = 0;
                    while (phones.moveToNext()) {
                        contactNumbers[count] = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        count++;
                    }
                    phones.close();

                    if (contactNumbers.length > 1 ) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(this);
                        builder.setTitle("Call Number");
                        builder.setItems(contactNumbers, new OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                ContactActivity.roamNumber
                                        .setText(contactNumbers[which]);

                            }
                        });

                        builder.create().show();

                    } else {
                        ContactActivity.roamNumber
                        .setText(contactNumbers[0]);
                     }


                } finally {
                    c.close();          
                      }
于 2012-10-01T15:18:15.083 に答える