android - java.lang.IllegalArgumentException : Invalid column data1 in onActivityResult()

标签 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 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();

                }

提前致谢。

最佳答案

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

@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();
        }
    }

关于android - java.lang.IllegalArgumentException : Invalid column data1 in onActivityResult(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29797849/

相关文章:

java - 如何去除中心点下方的阴影?

java - 如何使用 float 操作按钮(fab)创建新 Activity ?

java - 在 java 中从 hotmail gmail yahoo 获取联系人列表?

javascript - 将手机号码保存到 WP 联系人 wird cordova 联系人插件

android - 从服务 android 启动 Activity

android - 抽屉导航图标位置更改

java - Android:OnClick 按钮不会向 TextView 添加/删除值

android - 从 Android Froyo 2.2 检索联系人

java - URLConnection 超时问题

android - 通过 JNI 从 C++ 发送 Intent