这是我使用getApplicationContext()
public class SaveInSQLiteContacts {
private Context context;
public SaveInSQLiteContacts(Context context){
this.context = context;
}
public void save(String name, String imageKey, String email, String phone, String uid){
SQLiteContacts sqLiteContacts = new SQLiteContacts(context);
SQLiteDatabase database = sqLiteContacts.getWritableDatabase();
ContentValues contentValues = new ContentValues();
byte [] image = null;
if(imageKey != null)
image = Base64.decode(imageKey, Base64.DEFAULT);
contentValues.put(SQLiteContacts.KEY_NAME, name);
contentValues.put(SQLiteContacts.KEY_IMAGE, image);
contentValues.put(SQLiteContacts.KEY_EMAIL, email);
contentValues.put(SQLiteContacts.KEY_PHONE, phone);
contentValues.put(SQLiteContacts.USERID, uid);
database.insert(SQLiteContacts.CONTACTS, null, contentValues);
sqLiteContacts.close();
}
}
这就是我试图从中获取数据的方式
private void checker(){
SQLiteContacts sqLiteContacts = new SQLiteContacts(getApplicationContext());
SQLiteDatabase database = sqLiteContacts.getWritableDatabase();
Cursor cursor = database.query(SQLiteContacts.CONTACTS, null, null, null, null, null, null);
if(cursor != null) {
if (cursor.moveToFirst()) {
int i = cursor.getColumnIndex(SQLiteContacts.KEY_ID);
int itemId = cursor.getColumnIndex(SQLiteContacts.KEY_NAME);
int fixerId = cursor.getColumnIndex(SQLiteContacts.USERID);
do {
int id = cursor.getInt(i);
String itemName = cursor.getString(itemId);
String fixerUID = cursor.getString(fixerId);
Log.d("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", itemName);
Log.d("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", fixerUID);
Log.d("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", String.valueOf(id));
} while (cursor.moveToNext());
}
}
cursor.close();
sqLite.close();
}
这是我得到的:
Failed to read row 0, column 0 from a CursorWindow which has 0 rows, 6 columns.
java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
我做错了什么?
答案 0 :(得分:0)
查看logcat中的警告。
你有看起来像这样的东西吗?:
WARN CursorWindow窗口已满:请求分配x个字节,空闲 空格x字节,窗口大小x字节
这意味着您的光标对象大于分配的大小,通常为2Mb。
您保存的图像可能是超出此2Mb限制的原因。
也许您可以查询其他数据而不是图像,然后逐个加载图片。
如果您只需要代码中的三列,请尝试以下操作:
String[] columns = new String[] { SQLiteContacts.KEY_ID, SQLiteContacts.KEY_NAME, SQLiteContacts.USERID };
Cursor cursor = database.query(SQLiteContacts.CONTACTS, columns, null, null, null, null, null);
答案 1 :(得分:0)
请通过运行以下语句检查Logcat。可能是游标大小为0. Log.e("size",cursor.getCount());