我正在计算来自数据库(元素)的数据。但我在这一行遇到异常这是我的日志。
这是我的主要 java 文件,这里有两个保存 ok 日志,打印然后异常显示在计数行
package com.example.database_example;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Information inf=new Information("naveen");
Information inf2=new Information("Rvi");
DataBaseExample dbx=new DataBaseExample(MainActivity.this);
if(dbx.insertname(inf)){
Log.v("checkdbx.insertname(inf);", "save ok.");
}else{
Log.v("checkdbx.insertname(inf);", "save failed.");
}
if(dbx.insertname(inf2)){
Log.v("checkdbx.insertname(inf);", "save ok.");
}else{
Log.v("checkdbx.insertname(inf);", "save failed.");
}
Log.v("count", "mane ok."+dbx.getContactsCount());
List<Information> itemsList= dbx.getAllItems();
for (int i = 0; i < itemsList.size(); i++) {
Information inft= itemsList.get(i);
Log.v("name", "mane ok."+inft.getName()+inft.getKey());
}
}
}
和下面的错误..
09-24 09:38:51.288: V/checkdbx.insertname(inf);(674): save ok.
09-24 09:38:51.318: V/checkdbx.insertname(inf);(674): save ok.
09-24 09:38:51.328: D/AndroidRuntime(674): Shutting down VM
09-24 09:38:51.328: W/dalvikvm(674): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-24 09:38:51.348: E/AndroidRuntime(674): FATAL EXCEPTION: main
09-24 09:38:51.348: E/AndroidRuntime(674): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database_example/com.example.database_example.MainActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM Name)
09-24 09:38:51.348: E/AndroidRuntime(674): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-24 09:38:51.348: E/AndroidRuntime(674): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-24 09:38:51.348: E/AndroidRuntime(674): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-24 09:38:51.348: E/AndroidRuntime(674): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-24 09:38:51.348: E/AndroidRuntime(674): at android.os.Handler.dispatchMessage(Handler.java:99)
09-24 09:38:51.348: E/AndroidRuntime(674): at android.os.Looper.loop(Looper.java:123)
09-24 09:38:51.348: E/AndroidRuntime(674): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-24 09:38:51.348: E/AndroidRuntime(674): at java.lang.reflect.Method.invokeNative(Native Method)
09-24 09:38:51.348: E/AndroidRuntime(674): at java.lang.reflect.Method.invoke(Method.java:507)
09-24 09:38:51.348: E/AndroidRuntime(674): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-24 09:38:51.348: E/AndroidRuntime(674): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-24 09:38:51.348: E/AndroidRuntime(674): at dalvik.system.NativeStart.main(Native Method)
09-24 09:38:51.348: E/AndroidRuntime(674): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM Name)
09-24 09:38:51.348: E/AndroidRuntime(674): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:34)
09-24 09:38:51.348: E/AndroidRuntime(674): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:67)
09-24 09:38:51.348: E/AndroidRuntime(674): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:287)
09-24 09:38:51.348: E/AndroidRuntime(674): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:268)
09-24 09:38:51.348: E/AndroidRuntime(674): at com.example.database_example.DataBaseExample.getContactsCount(DataBaseExample.java:98)
09-24 09:38:51.348: E/AndroidRuntime(674): at com.example.database_example.MainActivity.onCreate(MainActivity.java:32)
09-24 09:38:51.348: E/AndroidRuntime(674): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-24 09:38:51.348: E/AndroidRuntime(674): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-24 09:38:51.348: E/AndroidRuntime(674): ... 11 more
package com.example.database_example;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseExample extends SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "information";
// Contacts table name
private static final String TABLE_Name= "Name";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
public DataBaseExample(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String createTable= "CREATE TABLE " + TABLE_Name+"("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME + " TEXT"
+ ")";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Name);
// Create tables again
onCreate(db);
}
public boolean insertname(Information information) {
boolean createSuccessful = false;
ContentValues values = new ContentValues();
// values.put(KEY_ID, information.getId());
values.put(KEY_NAME, information.getName());
SQLiteDatabase db = this.getWritableDatabase();
createSuccessful = db.insert(TABLE_Name, null, values) > 0;
db.close();
return createSuccessful;
}
public List<Information> getAllItems(){
List<Information> itemsList = new ArrayList<Information>();
Cursor cursor = null;
try {
//get all rows
SQLiteDatabase mDatabase = this.getReadableDatabase();
cursor = mDatabase.query(TABLE_Name, null, null, null, null,
null, null);
if (cursor.moveToFirst()) {
do {
Information c = new Information();
c.setName(cursor.getString(cursor.getColumnIndex((KEY_NAME))));
c.setKey(cursor.getString(cursor.getColumnIndex((KEY_ID))));
itemsList.add(c);
} while (cursor.moveToNext());
}
} catch (SQLiteException e) {
e.printStackTrace();
} finally {
cursor.close();
}
return itemsList;
}
public int getContactsCount() {
String countQuery = "SELECT * FROM " + TABLE_Name;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
package com.example.database_example;
import android.R.string;
public class Information {
String name;
String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Information(String name) {
// TODO Auto-generated constructor stub
this.name=name;
}
public Information() {
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}