java - SQLite 数据库 'does not implement interface'

标签 java android android-sqlite implements

当我使用 SQLiteDatabase 时出现此错误作为Closeable

我有一个示例项目可以重新创建它:

https://github.com/blundell/SQLDatabaseError

使用扩展 SQLiteOpenHelper 的类:

public class DatabaseHelper extends SQLiteOpenHelper {

....

public void openAndCloseDatabase() {
    SQLiteDatabase database = getWritableDatabase();

    close(database);
}

private void close(Closeable database) {
    try {
        if (database != null) {
            database.close();
        }
    } catch (Exception e) {
        Log.e("Error", "Oh no!", e);
    }
}

}

堆栈跟踪:

12-14 12:23:43.719: E/AndroidRuntime(5179): FATAL EXCEPTION: main
12-14 12:23:43.719: E/AndroidRuntime(5179): java.lang.IncompatibleClassChangeError: interface not implemented
12-14 12:23:43.719: E/AndroidRuntime(5179):   at com.blundell.sqldatabasecursorerror.DatabaseHelper.close(DatabaseHelper.java:35)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at com.blundell.sqldatabasecursorerror.DatabaseHelper.openAndCloseDatabase(DatabaseHelper.java:29)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at com.blundell.sqldatabasecursorerror.MainActivity.onCreate(MainActivity.java:13)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at android.app.ActivityThread.access$1500(ActivityThread.java:121)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at android.os.Looper.loop(Looper.java:130)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at android.app.ActivityThread.main(ActivityThread.java:3701)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at java.lang.reflect.Method.invokeNative(Native Method)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at java.lang.reflect.Method.invoke(Method.java:507)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
12-14 12:23:43.719: E/AndroidRuntime(5179):     at dalvik.system.NativeStart.main(Native Method)

应用程序接口(interface):

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html

http://developer.android.com/reference/android/database/sqlite/SQLiteClosable.html

http://developer.android.com/reference/java/io/Closeable.html

这应该行得通吧?

 public final class SQLiteDatabase extends SQLiteClosable
 >>
 public abstract class SQLiteClosable extends Object implements Closeable
 >>
 public interface Closeable

不起作用:

  • Xperia Play Android 2.3.4
  • 摩托罗拉 Xoom Android 4.0.4

有效:

  • 三星 Galaxy Nexus Android 4.2

最佳答案

好的,找到问题了in the Change Notes for 4.1 (API Level 16) :

android.database.sqlite.SQLiteClosable 仅从 API 级别 16 实现 java.io.Closeable。在此之前,它们是不相关的(即使自从开始)。

所以如果你想让你的代码向后兼容,你应该直接使用 SQLiteClosable。

也可能值得为您想要支持的最低版本安装 SDK 并尝试使用它进行构建,这样编译器就会检测到这一点。

关于java - SQLite 数据库 'does not implement interface',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13878908/

相关文章:

java Swing ;如何切换面板的可见性?

java - Android SQLite 从 fragment 调用数据库

android - 将 sqlite 查询打印到 logCat

php - 将参数从 Android 传递到 php

java - 如何回滚android sqlite数据库中已删除的行?

java - Spring Boot 嵌入式 Tomcat 应用程序作为 Windows exe 文件

java - Jsoup 错误?解析 .mht 文档

java - 泛型中原始类型和 <?> 的区别

java - 如何迭代 Android JavascriptInterface 返回的项目?

android - 在 android 项目中使用 Telegram tdlib