public Cursor getsomething()
{
this.open();
Cursor c= database.query(DatabaseHandler.Table_Name,magicdata,null, null, null,null,null);
this.close();
return c;
}
我正在使用此代码查询我的数据库。然后我在主要活动中称之为。
get=X.getsomething();
if (get.getCount() > 0)
{
if (get.moveToFirst())
{
do {
String uname = get.getString(get.getColumnIndex("content_id"));
String unknown =url + uname;
String ax= get.getString(get.getColumnIndex("content"));
downloadimages(url,ax);
}
while (get.moveToNext());
}
}
}}
我在这里得到error
,我的代码没有运行,我不知道为什么?
错误代码在
之下 02-20 10:27:18.378: E/PhonePolicy(1261): Could not preload class for phone policy com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
02-20 10:27:18.488: D/TextLayoutCache(1261): Using debug level: 0 - Debug Enabled: 0
02-20 10:27:20.998: D/AndroidRuntime(1261): Shutting down VM
02-20 10:27:20.998: W/dalvikvm(1261): threadid=1: thread exiting with uncaught exception (group=0x40a3e1f8)
02-20 10:27:20.998: E/AndroidRuntime(1261): FATAL EXCEPTION: main
02-20 10:27:20.998: E/AndroidRuntime(1261): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newpicker/com.example.newpicker.MainActivity}: java.lang.IllegalStateException: database /data/data/com.example.newpicker/databases/content.db (conn# 0) already closed
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.os.Looper.loop(Looper.java:137)
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.app.ActivityThread.main(ActivityThread.java:4429)
02-20 10:27:20.998: E/AndroidRuntime(1261): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 10:27:20.998: E/AndroidRuntime(1261): at java.lang.reflect.Method.invoke(Method.java:511)
02-20 10:27:20.998: E/AndroidRuntime(1261): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
02-20 10:27:20.998: E/AndroidRuntime(1261): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
02-20 10:27:20.998: E/AndroidRuntime(1261): at dalvik.system.NativeStart.main(Native Method)
02-20 10:27:20.998: E/AndroidRuntime(1261): Caused by: java.lang.IllegalStateException: database /data/data/com.example.newpicker/databases/content.db (conn# 0) already closed
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082)
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:413)
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:400)
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:79)
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:164)
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:156)
02-20 10:27:20.998: E/AndroidRuntime(1261): at com.example.newpicker.MainActivity.datamining(MainActivity.java:70)
02-20 10:27:20.998: E/AndroidRuntime(1261): at com.example.newpicker.MainActivity.onCreate(MainActivity.java:45)
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.app.Activity.performCreate(Activity.java:4465)
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-20 10:27:20.998: E/AndroidRuntime(1261): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-20 10:27:20.998: E/AndroidRuntime(1261): ... 11 more
答案 0 :(得分:1)
这是您的解决方案。试试这个。
String[] columnArray = { "content_id, content " };
Cursor cursor = database.query(DatabaseHelper.TABLE_NAME,columnArray, null, null, null, null, null);
if (cursor.getCount() > 0) {
cursor.moveToFirst();
do {
int id = cursor.getInt(cursor.getColumnIndex("content_id"));
String content =cursor.getString(cursor.getColumnIndex("content"));
} while (cursor.moveToNext());
cursor.close();
} else {
Log.i("TAG"," No value found");
}
答案 1 :(得分:1)
阅读行
02-20 10:27:20.998:E / AndroidRuntime(1261):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.newpicker / com.example.newpicker.MainActivity}:java.lang。 IllegalStateException:数据库/data/data/com.example.newpicker/databases/content.db(conn#0)已经关闭
从日志中我们看到database already closed
所以你需要做的是:
打开数据库并确保在运行游标代码之前它不会关闭:
public Cursor getsomething()
{
// this.open(); // <-- explained below
Cursor c= database.query(DatabaseHandler.Table_Name,magicdata,null, null, null,null,null);
// this.close(); // <-- if `this` refers to DB, it's a mistake, close DB at the end.
return c;
}
因此,在您的活动代码中,为了不打开您的数据库,请使用以下内容:
X = new YourDatabaseClassName(); // new instance of your database (class contains getsomething method)
X.open(); // open DB
get=X.getsomething();
if (get != null && get.getCount() > 0) // also add a null check
{
if (get.moveToFirst())
{
do {
String uname = get.getString(get.getColumnIndex("content_id"));
String unknown =url + uname;
String ax= get.getString(get.getColumnIndex("content"));
downloadimages(url,ax);
}
while (get.moveToNext());
}
get.close(); // also close the cursor
}
X.close(); // you end with DB so close it! next time it needs to reopened
Here也是一个android数据库教程。