java.lang.IllegalArgumentException:onActivityResult()

时间:2015-04-22 12:38:34

标签: android android-intent contacts onactivityresult

有人可以告诉我为什么会得到:

java.lang.IllegalArgumentException: Invalid column data1
04-22 13:42:47.258: E/AndroidRuntime(21269):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3351)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3394)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at android.app.ActivityThread.access$1300(ActivityThread.java:135)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at android.os.Looper.loop(Looper.java:136)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at android.app.ActivityThread.main(ActivityThread.java:5001)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at java.lang.reflect.Method.invokeNative(Native Method)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at java.lang.reflect.Method.invoke(Method.java:515)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at dalvik.system.NativeStart.main(Native Method)
04-22 13:42:47.258: E/AndroidRuntime(21269): Caused by: java.lang.IllegalArgumentException: Invalid column data1
04-22 13:42:47.258: E/AndroidRuntime(21269):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at android.content.ContentProviderProxy.query(ContentProviderNative.java:413)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at android.content.ContentResolver.query(ContentResolver.java:461)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at android.content.ContentResolver.query(ContentResolver.java:404)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at de.myapp.main.MainActivity.onActivityResult(MainActivity.java:252)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at android.app.Activity.dispatchActivityResult(Activity.java:5423)
04-22 13:42:47.258: E/AndroidRuntime(21269):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3347)
04-22 13:42:47.258: E/AndroidRuntime(21269):    ... 11 more
04-22 13:42:47.288: D/dalvikvm(21269): GC_FOR_ALLOC freed 304K, 11% free 10547K/11756K, paused 17ms, total 17ms
04-22 13:42:50.081: I/Process(21269): Sending signal. PID: 21269 SIG: 9

这是我的意图,以便添加联系人:

                Intent intent = new Intent(Intent.ACTION_INSERT, 
                                   ContactsContract.Contacts.CONTENT_URI);
                if (Integer.valueOf(Build.VERSION.SDK) > 14){
                    intent.putExtra("finishActivityOnSaveCompleted", true); // Fix for 4.0.3 
                }    
                startActivityForResult(intent, ADDED_CONTACT);

我正在尝试添加联系人并将插入详细信息作为返回值返回我的应用。

这是我的onActivityResult():

 else if (requestCode == ADDED_CONTACT){
                    // Get the URI that points to the selected contact
                    Uri contactUri = intent.getData();
                    // We only need the NUMBER column, because there will be only one row in the result
                    String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};

                    // Perform the query on the contact to get the NUMBER column
                    // We don't need a selection or sort order (there's only one result for the given URI)
                    // CAUTION: The query() method should be called from a separate thread to avoid blocking
                    // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
                    // Consider using CursorLoader to perform the query.
                    Cursor cursor = getContentResolver()
                            .query(contactUri, projection, null, null, null);
                    cursor.moveToFirst();

                    // Retrieve the phone number from the NUMBER column
                    int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    String number = cursor.getString(column);
                    // Retrieve the contact name from the DISPLAY_NAME column
                    int column_name = cursor.getColumnIndex(Phone.DISPLAY_NAME);
                    String name = cursor.getString(column_name);

                    // Do something with the phone number...
                    Toast.makeText(this, "I added the Contact: \n"+name+" "+number, Toast.LENGTH_SHORT).show();

                }

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以使用此代码段添加联系人,然后在您的应用中检索它。

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1)
        {
            // Get the URI that points to the selected contact
            Uri contactUri = data.getData();
            // We only need the NUMBER column, because there will be only one row in the result
            String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.CONTACT_ID};

            String[] segments = contactUri.toString().split("/");
            String id = segments[segments.length - 1];

            // Perform the query on the contact to get the NUMBER column
            // We don't need a selection or sort order (there's only one result for the given URI)
            // CAUTION: The query() method should be called from a separate thread to avoid blocking
            // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
            // Consider using CursorLoader to perform the query.
            Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null, null, null);
            cursor.moveToFirst();
            while (!cursor.isAfterLast())
            {
                int cid = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
                String contactid = cursor.getString(cid);

                if (contactid.equals(id))
                {
                    // Retrieve the phone number from the NUMBER column
                    int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                    String number = cursor.getString(column);

                    // Retrieve the contact name from the DISPLAY_NAME column
                    int column_name = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                    String name = cursor.getString(column_name);

                    // Do something with the phone number...
                    Toast.makeText(this, "I added the Contact: \n" + name + " " + number, Toast.LENGTH_SHORT).show();
                }


                cursor.moveToNext();
            }

            cursor.close();
        }
    }