我收到错误无法从 CursorWindow 读取第 0 行第 9 列。确保在从光标访问数据之前正确初始化光标。另外两个人能够毫无错误地运行代码,但在我的机器上它会抛出它。我很困惑。这是处理 SQLite 的代码:在此先感谢,抱歉有很多代码
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.SQLiteOpenHelper;
import android.util.Log;
public class FeedSQLiteHelper extends SQLiteOpenHelper {
//Table Name
public static final String TABLE_POSTS = "comments";
//Table Column names
public static final String COLUMN_ID = "_id";
public static final String COLUMN_CAPTION = "caption";
public static final String COLUMN_NAME= "name";
public static final String COLUMN_LIKE="like";
public static final String COLUMN_DISLIKE="dislike";
public static final String COLUMN_COMMENTS = "columns";
public static final String COLUMN_ALL_COMMENTS = "allComments";
public static final String COLUMN_PHOTO = "photo";
public static final String COLUMN_CELEB = "celeb";
public static final String COLUMN_FID = "facebook_id";
public static final String COLUMN_TIME = "timestamp";
private static final String DATABASE_NAME = "commments.db";
private static final int DATABASE_VERSION = 6;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_POSTS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_CAPTION
+ " text not null, " + COLUMN_NAME + " text not null, "
+ COLUMN_LIKE + " integer not null, " + COLUMN_DISLIKE + " integer not null, "
+ COLUMN_COMMENTS + " integer not null, " + COLUMN_ALL_COMMENTS + " text, "
+ COLUMN_PHOTO + " text, " + COLUMN_FID + " text, " + COLUMN_TIME + " long);";
public FeedSQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(FeedSQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
if(oldVersion <6) {
final String ALTER_TBL = "ALTER TABLE " + TABLE_POSTS + " ADD COLUMN " + COLUMN_TIME + " long;";
db.execSQL(ALTER_TBL);
}
}
//Adding new contact
public void addContact(FeedsModel contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, contact.getName());
values.put(COLUMN_CAPTION, contact.getDesc());
values.put(COLUMN_LIKE, contact.getUps());
values.put(COLUMN_DISLIKE, contact.getDowns());
values.put(COLUMN_COMMENTS, contact.getComments());
values.put(COLUMN_ALL_COMMENTS, contact.getAllComments());
values.put(COLUMN_PHOTO, contact.getImageId());
values.put(COLUMN_FID, contact.getFID());
values.put(COLUMN_TIME, contact.getTimestamp());
// Inserting Row
db.insert(TABLE_POSTS, null, values);
db.close(); // Closing database connection
}
//Getting single contact
public FeedsModel getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_POSTS, new String[] { COLUMN_ID,
COLUMN_CAPTION, COLUMN_NAME, COLUMN_LIKE, COLUMN_DISLIKE, COLUMN_COMMENTS, COLUMN_ALL_COMMENTS, COLUMN_PHOTO, COLUMN_FID, COLUMN_TIME }, COLUMN_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
FeedsModel contact = new FeedsModel(
Integer.parseInt(cursor.getString(0)),
cursor.getString(1),
cursor.getString(2),
Integer.parseInt(cursor.getString(3)),
Integer.parseInt(cursor.getString(4)),
Integer.parseInt(cursor.getString(5)),
cursor.getString(6),
cursor.getString(7),
cursor.getString(8),
Long.parseLong(cursor.getString(9)));
// return contact
return contact;
}
//filters the news feed for the 'Me' option
public List<FeedsModel> getMe(String me)
{
List<FeedsModel> contactList = new ArrayList<FeedsModel>();
String selectQuery = "SELECT * FROM " + TABLE_POSTS + " WHERE " + COLUMN_FID + "= "+ me;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
FeedsModel contact = new FeedsModel();
contact.setId(Integer.parseInt(cursor.getString(0)));
contact.setDesc(cursor.getString(1));
contact.setName(cursor.getString(2));
contact.setUps(Integer.parseInt(cursor.getString(3)));
contact.setDowns(Integer.parseInt(cursor.getString(4)));
contact.setComments(Integer.parseInt(cursor.getString(5)));
contact.setAllComments(cursor.getString(6));
contact.setImageId(cursor.getString(7));
contact.setFID(cursor.getString(8));
contact.setTimestamp(Long.parseLong(cursor.getString(9)));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
//filters the news feed for the 'Me' option
public List<FeedsModel> getMostRecent()
{
List<FeedsModel> contactList = new ArrayList<FeedsModel>();
String selectQuery = "SELECT * FROM " + TABLE_POSTS + " ORDER BY " + COLUMN_TIME + " DESC";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
FeedsModel contact = new FeedsModel();
contact.setId(Integer.parseInt(cursor.getString(0)));
contact.setDesc(cursor.getString(1));
contact.setName(cursor.getString(2));
contact.setUps(Integer.parseInt(cursor.getString(3)));
contact.setDowns(Integer.parseInt(cursor.getString(4)));
contact.setComments(Integer.parseInt(cursor.getString(5)));
contact.setAllComments(cursor.getString(6));
contact.setImageId(cursor.getString(7));
contact.setFID(cursor.getString(8));
contact.setTimestamp(Long.parseLong(cursor.getString(9)));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
//Getting All Contacts
public List<FeedsModel> getAllContacts() {
List<FeedsModel> contactList = new ArrayList<FeedsModel>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_POSTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
FeedsModel contact = new FeedsModel();
contact.setId(Integer.parseInt(cursor.getString(0)));
contact.setDesc(cursor.getString(1));
contact.setName(cursor.getString(2));
contact.setUps(Integer.parseInt(cursor.getString(3)));
contact.setDowns(Integer.parseInt(cursor.getString(4)));
contact.setComments(Integer.parseInt(cursor.getString(5)));
contact.setAllComments(cursor.getString(6));
contact.setImageId(cursor.getString(7));
contact.setFID(cursor.getString(8));
contact.setTimestamp(Long.parseLong(cursor.getString(9)));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
//Getting contacts Count
public int getContactsCount() {
String countQuery = "SELECT * FROM " + TABLE_POSTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
//Updating single contact
public int updateContact(FeedsModel contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, contact.getName());
values.put(COLUMN_CAPTION, contact.getDesc());
values.put(COLUMN_LIKE, contact.getUps());
values.put(COLUMN_DISLIKE, contact.getDowns());
values.put(COLUMN_COMMENTS, contact.getComments());
values.put(COLUMN_ALL_COMMENTS, contact.getAllComments());
values.put(COLUMN_PHOTO, contact.getImageId());
values.put(COLUMN_FID, contact.getFID());
values.put(COLUMN_TIME, contact.getTimestamp());
// updating row
return db.update(TABLE_POSTS, values, COLUMN_ID + " = ?",
new String[] { String.valueOf(contact.getId()) });
}
//Deleting single contact
public void deleteContact(FeedsModel contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_POSTS, COLUMN_ID + " = ?",
new String[] { String.valueOf(contact.getId()) });
db.close();
}
}
日志猫:
E/CursorWindow(841): Failed to read row 0, column 9 from a CursorWindow which has 3 rows, 9 columns.
D/AndroidRuntime(841): Shutting down VM
W/dalvikvm(841): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
E/AndroidRuntime(841): FATAL EXCEPTION: main
E/AndroidRuntime(841): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.Drake.doppelganger/edu.Drake.doppelganger.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col 9 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
E/AndroidRuntime(841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
E/AndroidRuntime(841): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime(841): at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime(841): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime(841): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(841): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(841): at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime(841): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(841): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(841): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(841): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(841): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(841): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 9 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
E/AndroidRuntime(841): at android.database.CursorWindow.nativeGetString(Native Method)
E/AndroidRuntime(841): at android.database.CursorWindow.getString(CursorWindow.java:434)
E/AndroidRuntime(841): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
E/AndroidRuntime(841): at edu.Drake.doppelganger.FeedSQLiteHelper.getAllContacts(FeedSQLiteHelper.java:198)
E/AndroidRuntime(841): at edu.Drake.doppelganger.FeedFragment.onActivityCreated(FeedFragment.java:65)
E/AndroidRuntime(841): at android.app.Fragment.performActivityCreated(Fragment.java:1703)
E/AndroidRuntime(841): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
E/AndroidRuntime(841): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
E/AndroidRuntime(841): at android.app.BackStackRecord.run(BackStackRecord.java:682)
E/AndroidRuntime(841): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
E/AndroidRuntime(841): at android.app.Activity.performStart(Activity.java:5113)
E/AndroidRuntime(841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
E/AndroidRuntime(841): ... 11 more