0

我正在以编程方式添加新联系人,该联系人又返回 contentProviderResults。

我从 contentProviderResults 获取联系人 URi 并将其传递给Intent.action_view

Uri newContactUri = contentProviderResults[0].uri;
Log.i("contact", "New contact Uri is " + newContactUri);
Intent intent = new Intent(Intent.ACTION_VIEW, newContactUri);

我面临的问题是,除了三星(运行 OS < 4.0)(即 ICS 的前身)之外的所有设备上都能完美运行

在三星 4.0 之前的设备上,acore 进程停止并引发强制关闭对话框。

不幸的是 process.android.process.acore 已经停止了......

插入联系人后收到的示例 Uri 是

01-01 06:25:40.233: I/contact(2803): New contact Uri is content://com.android.contacts/raw_contacts/56

以下是我添加联系人并显示添加的联系人详细信息的方法。

 public void insertContact(Context context, String displayName,
                String mobileNumber) {

            ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

            ops.add(ContentProviderOperation
                    .newInsert(ContactsContract.RawContacts.CONTENT_URI)
                    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                    .build());

            ops.add(ContentProviderOperation
                    .newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                    .withValue(
                            ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(
                            ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
                            displayName).build());

            ops.add(ContentProviderOperation
                    .newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                    .withValue(
                            ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
                            mobileNumber)
                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                            ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                    .build());

            try {
                final ContentProviderResult[] contentProviderResults = context
                        .getContentResolver().applyBatch(
                                ContactsContract.AUTHORITY, ops);
                if (contentProviderResults != null
                        && contentProviderResults[0] != null) {
                    Toast.makeText(context, "Contact Added Successfully",
                            Toast.LENGTH_LONG).show();

                    for (int i = 0; i < contentProviderResults.length; i++)
                        Log.i("contact", "" + contentProviderResults[i].uri);

                    Uri newContactUri = contentProviderResults[0].uri;

                    Log.i("contact", "New contact Uri is " + newContactUri);
                    Intent intent = new Intent(Intent.ACTION_VIEW, newContactUri);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);

                }

            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(context, "Exception: " + e.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }

        }

有人可以告诉我为什么这个代码在三星设备上失败(Pre 4.0 版本)。

下面是崩溃日志。

01-01 06:25:40.978: E/AndroidRuntime(2894): FATAL EXCEPTION: main
01-01 06:25:40.978: E/AndroidRuntime(2894): java.lang.RuntimeException: Unable to resume activity {com.android.contacts/com.sec.android.app.contacts.PhoneBookDetailTabActivity}: java.lang.IllegalArgumentException: Invalid column lookup
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.os.Looper.loop(Looper.java:123)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at java.lang.reflect.Method.invokeNative(Native Method)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at java.lang.reflect.Method.invoke(Method.java:521)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at dalvik.system.NativeStart.main(Native Method)
01-01 06:25:40.978: E/AndroidRuntime(2894): Caused by: java.lang.IllegalArgumentException: Invalid column lookup
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.database.sqlite.SQLiteQueryBuilder.computeProjection(SQLiteQueryBuilder.java:523)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.database.sqlite.SQLiteQueryBuilder.buildQuery(SQLiteQueryBuilder.java:370)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:323)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:7846)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:7831)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.content.ContentProvider$Transport.query(ContentProvider.java:163)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.content.ContentResolver.query(ContentResolver.java:245)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.provider.ContactsContract$Contacts.getLookupUri(ContactsContract.java:916)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at com.sec.android.app.contacts.PhoneBookDetailTabActivity.startQuery(PhoneBookDetailTabActivity.java:522)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at com.sec.android.app.contacts.PhoneBookDetailTabActivity.onResume(PhoneBookDetailTabActivity.java:361)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.app.Activity.performResume(Activity.java:3823)
01-01 06:25:40.978: E/AndroidRuntime(2894):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
01-01 06:25:40.978: E/AndroidRuntime(2894):     ... 12 more
4

0 回答 0