Blu*_*ell 7 java android implements android-sqlite
使用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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
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)
Run Code Online (Sandbox Code Playgroud)
API:
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
Run Code Online (Sandbox Code Playgroud)
不起作用:
工作:
好的,在4.1的更改说明(API级别16)中发现了问题:
android.database.sqlite.SQLiteClosablejava.io.Closeable仅从API级别16 实现.在此之前,它们是不相关的(即使两个接口从一开始就存在).
因此,如果您希望代码向后兼容,则应直接使用SQLiteClosable.
也可能值得为您想要支持的最低版本安装SDK并尝试使用它构建,然后编译器会检测到这一点.
| 归档时间: |
|
| 查看次数: |
753 次 |
| 最近记录: |