创建Android联系人组时出现奇怪的例外情况

时间:2015-08-01 07:52:07

标签: android android-contacts

最近看了我的谷歌分析页面,我看到有时会引发一个例外,我无法解决它,因为我无法重现它。代码的想法是检查是否存在自定义联系人组,如果不存在,则创建它。代码如下:

public static long ensureSampleGroupExists(Context context, Account account)    {
    Log.v(TAG, "ensureSampleGroupExists");

    final ContentResolver resolver = context.getContentResolver();

    // Lookup the sample group
    long groupId = 0;
    final Cursor cursor = resolver.query(Groups.CONTENT_URI,
            new String[] { Groups._ID },
            Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE
                    + "=? AND " + Groups.TITLE + "=?", new String[] {
                    account.name, account.type, SAMPLE_GROUP_NAME }, null);
    if (cursor != null) {
        try {
            if (cursor.moveToFirst()) {
                groupId = cursor.getLong(0);
            }
        } finally {
            cursor.close();
        }
    }

    if (groupId == 0) {
        // Sample group doesn't exist yet, so create it
        final ContentValues contentValues = new ContentValues();
        contentValues.put(Groups.ACCOUNT_NAME, account.name);
        contentValues.put(Groups.ACCOUNT_TYPE, account.type);
        contentValues.put(Groups.TITLE, SAMPLE_GROUP_NAME);
        contentValues.put(Groups.GROUP_IS_READ_ONLY, true);

        final Uri newGroupUri = resolver.insert(Groups.CONTENT_URI,
                contentValues); //<-- NULLPOINTER EXCEPTION IN SOME DEVICES (or at least 1 :P)
        groupId = ContentUris.parseId(newGroupUri);
    }
    return groupId;
}

我可以在google analytics网页上看到以下异常 -

NullPointerException (@ContactManager:ensureSampleGroupExists:95) {SyncAdapterThread-1}

不确定问题是否是ContentResolver但是在检查时它不是null(上下文都不为空)所以我有点困惑......

不幸的是没有错误报告,所以我没有堆栈跟踪:(,有人对可能发生的事情有任何线索吗?任何帮助都将受到高度赞赏

谢谢

编辑

我设法通过GA 4获得完整的堆栈跟踪,检查出来

java.lang.NullPointerException at
android.os.Parcel.readException(Parcel.java:1478)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185) 
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137) 
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:468) 
at android.content.ContentResolver.insert(ContentResolver.java:1190) 
at com.smm.epctrackerapp.sqlite.ContactManager.ensureSampleGroupExists(ContactManager.java:96) 
at com.smm.epctrackerapp.adapter.ContactSyncAdapter.onPerformSync(ContactSyncAdapter.java:72)
at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:259)

0 个答案:

没有答案