嗨,在我的应用程序中,它显示错误。我的应用程序在所有模拟器中运行,除了一个运行意味着它显示日志猫错误,任何人都可以告诉我如何解决我的问题。
DatabaseHandlers类
public class DatabaseHandlers extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "FristRegistrationManager";
// GSfeedbacks table name
private static final String TABLE_CONTACTS = "firstregistration";
// GSfeedbacks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
private static final String KEY_PH_NO = "number";
public DatabaseHandlers(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_EMAIL + " TEXT,"
+ KEY_PH_NO + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addGSfeedback(GSfeedback contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // GSfeedback Name
values.put(KEY_EMAIL, contact.getEmail()); // GSfeedback Name
values.put(KEY_PH_NO, contact.getPhoneNumber()); // GSfeedback Phone
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
// Getting single contact
GSfeedback getGSfeedback(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME,KEY_EMAIL, KEY_PH_NO }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
GSfeedback contact = new GSfeedback(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),cursor.getString(3));
// return contact
return contact;
}
// Getting All GSfeedbacks
public ArrayList<GSfeedback> getAllGSfeedbacks() {
ArrayList<GSfeedback> contactList = new ArrayList<GSfeedback>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
GSfeedback contact = new GSfeedback();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setEmail(cursor.getString(2));
contact.setPhoneNumber(cursor.getString(3));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
// Updating single contact
public int updateGSfeedback(GSfeedback contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_EMAIL, contact.getEmail());
values.put(KEY_PH_NO, contact.getPhoneNumber());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
// Deleting single contact
public void deleteGSfeedback(GSfeedback contact) {
SQLiteDatabase db = this.getWritableDatabase();
//db.delete(TABLE_CONTACTS, KEY_ID + " = ?", new String[] { String.valueOf(contact.getID()) });
db.delete(TABLE_CONTACTS, null, null);
db.close();
}
// Getting contacts Count
public int getGSfeedbacksCount() {
String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
logcat的
06-24 19:01:00.203: E/Database(592): close() was never explicitly called on database '/data/data/com.agilerise.hotel/databases/FristRegistrationManager'
06-24 19:01:00.203: E/Database(592): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
06-24 19:01:00.203: E/Database(592): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
06-24 19:01:00.203: E/Database(592): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
06-24 19:01:00.203: E/Database(592): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
06-24 19:01:00.203: E/Database(592): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
06-24 19:01:00.203: E/Database(592): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:547)
06-24 19:01:00.203: E/Database(592): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
06-24 19:01:00.203: E/Database(592): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
06-24 19:01:00.203: E/Database(592): at com.agilerise.hotel.DatabaseHandlers.getAllGSfeedbacks(DatabaseHandlers.java:92)
06-24 19:01:00.203: E/Database(592): at com.agilerise.hotel.First.onCreate(First.java:22)
06-24 19:01:00.203: E/Database(592): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-24 19:01:00.203: E/Database(592): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
06-24 19:01:00.203: E/Database(592): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
06-24 19:01:00.203: E/Database(592): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-24 19:01:00.203: E/Database(592): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
06-24 19:01:00.203: E/Database(592): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 19:01:00.203: E/Database(592): at android.os.Looper.loop(Looper.java:130)
06-24 19:01:00.203: E/Database(592): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-24 19:01:00.203: E/Database(592): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 19:01:00.203: E/Database(592): at java.lang.reflect.Method.invoke(Method.java:507)
06-24 19:01:00.203: E/Database(592): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-24 19:01:00.203: E/Database(592): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-24 19:01:00.203: E/Database(592): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
尝试在完成或管理后关闭所有游标
接近:
cursor.close()
托管游标
startManagingCursor(cursor)
认为这应该对你有帮助!
答案 1 :(得分:0)
当你打开你的桌子和光标时,你必须要关闭它bcz避免任何潜在的内存泄漏,所以你不应该在不再使用时关闭它。请尝试以下代码: -
cursor.close();