1

我的代码是:

 public static Uri addCall(CallerInfo ci, Context context, String number,boolean isPrivateNumber, int callType, long start, int duration) {

 final ContentResolver resolver = context.getContentResolver();

                if (TextUtils.isEmpty(number)) {
                    if (isPrivateNumber) {
                        number = CallerInfo.PRIVATE_NUMBER;
                    } else {
                        number = CallerInfo.UNKNOWN_NUMBER;
                    }
                }

                ContentValues values = new ContentValues(5);

                if (number.contains("&"))
                    number = number.substring(0,number.indexOf("&"));
                values.put(Calls.NUMBER, number);
                values.put(Calls.TYPE, Integer.valueOf(callType));
                values.put(Calls.DATE, Long.valueOf(start));
                values.put(Calls.DURATION, Long.valueOf(duration));
                values.put(Calls.NEW, Integer.valueOf(1));
                if (ci != null) {
                    values.put(Calls.CACHED_NAME, ci.name);
                    values.put(Calls.CACHED_NUMBER_TYPE, ci.numberType);
                    values.put(Calls.CACHED_NUMBER_LABEL, ci.numberLabel);
                }

                if ((ci != null) && (ci.person_id > 0)) {
                    People.markAsContacted(resolver, ci.person_id);
                }

                Uri result = resolver.insert(Calls.CONTENT_URI, values);

                if (result != null) { // send info about call to call meter
                    final Intent intent = new Intent(ACTION_CM_SIP);
                    intent.putExtra(EXTRA_SIP_URI, result.toString());
                    // TODO: add provider
                    // intent.putExtra(EXTRA_SIP_PROVIDER, null);
                    context.sendBroadcast(intent);
                }

                return result;
            }

但是会抛出这样的异常:

07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: INSERT INTO logs ('_id', 'number', 'address', 'date', 'duration', 'type', 'new', 'name', 'name_rever
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:62)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:100)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308):     at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1254)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308):     at android.database.DatabaseUtils$InsertHelper.getStatement(DatabaseUtils.java:858)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308):     at android.database.DatabaseUtils$InsertHelper.insertInternal(DatabaseUtils.java:878)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308):     at android.database.DatabaseUtils$InsertHelper.insert(DatabaseUtils.java:1011)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308):     at com.android.providers.contacts.CallLogProvider.insert(CallLogProvider.java:240)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308):     at android.content.ContentProvider$Transport.insert(ContentProvider.java:174)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:146)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308):     at android.os.Binder.execTransact(Binder.java:288)
07-11 14:36:36.515: WARN/SQLiteCompiledSql(3308):     at dalvik.system.NativeStart.run(Native Method)

请提供解决方案来解决这个问题

4

2 回答 2

0

首先,您实例化大小为 5 的 ContentValues,但如果 ci != null 则您尝试添加 8 个项目。

于 2011-07-11T11:36:08.750 回答
0

我也发现了这个问题,虽然我发现堆栈跟踪有点偏离,但实际问题是因为当我的应用程序/活动停止时我没有关闭数据库。

在我的数据库助手类中,我有一个在退出活动之前运行的关闭方法:

public void close() {
        database.close();
    }

其中数据库是我正在运行查询的 SQLiteDatabase。

于 2011-07-30T20:33:38.840 回答