android - 调试 OperationApplicationException 插入失败

标签 android sqlite debugging batch-processing android-contentresolver

我将尝试尽可能简化这种情况。

观察以下代码:

//... 
ArrayList<ContentProviderOperation> batch = Lists.newArrayList();

while(recordsToProcess){
    //... Grabbing data
    //... built a ContentProviderOperation variable named operation

    batch.add(operation);

    //... setting recordsToProcess to true or false
}

try {
    mContext.getContentResolver().applyBatch(Contract.CONTENT_AUTHORITY, batch);

    } catch (RemoteException e) {
        throw new RuntimeException("Problem applying batch operation", e);
    } catch (OperationApplicationException e) {
        throw new RuntimeException("Problem applying batch operation", e);
    }
}

在此过程中,会抛出OperationApplicationException,如下所示:

... Problem applying batch operation
... android.content.OperationApplicationException: insert failed
... Stack Trace ....

据我所知,“如果插入失败或受影响的行数与预期计数不匹配,则抛出此异常”和/或“当 ContentProviderOperation 的应用程序由于指定的约束而失败时抛出。”

我的数据库模式几乎没有任何约束,并且操作中的数据是有效的。

有什么方法可以获取有关抛出异常原因的更多信息吗?什么约束失败了?等等...?

最佳答案

为了调试,我会从这样的事情开始

   {
   .........
        int i = 0;
        for (ContentProviderOperation op : ops) {
            applySingle(WorkoutProvider.AUTHORITY, op);
            Log.e(getTag(), Integer.toString(i));
            i++;
        }
    }


protected synchronized void applySingle(String authority, ContentProviderOperation op) {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(op);
    try {
        App.getInstance().getContentResolver().applyBatch(authority, ops);
    } catch (RemoteException e) {
        Log.e(getTag(), "Error updating workout id to server's", e);
        e.printStackTrace();
    } catch (OperationApplicationException e) {
        Log.e(getTag(), "Error updating workout id to server's", e);
        e.printStackTrace();
    }
}

然后我建议在 for 循环之前进行调试并查看失败的操作。

关于android - 调试 OperationApplicationException 插入失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16705083/

相关文章:

python - 如何对字段值的每个给定增量进行 GROUP BY?

python - SQL在特定列中插入多个数据(python)

java - 安卓 SQlite : Insert data into multiple tables with sharing ids

c# - 如何在 Visual Studio 调试器中查看位图对象?

android - 单击 fragment 会调用其背后的 Activity

android - 合并多个mp4音频文件-Android

javascript - JSLint:设置控制台时出现只读错误,即使它被设置为可写全局

debugging - 如果图像在只读内存中,调试器如何设置断点?

android - Ontouch() 不适用于自定义 View ?

java - 如何在 Android Studio 中单击按钮时创建一个新按钮?