我收到错误,无法从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 namepublic static final String table_posts = "comments";//table Column namespublic 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 statementprivate 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);}@OverrIDepublic voID onCreate(sqliteDatabase database) {database.execsql(DATABASE_CREATE);}@OverrIDepublic 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 contactpublic 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 Rowdb.insert(table_posts, null, values);db.close(); // Closing database connection}//Getting single contactpublic 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 contactreturn contact;}//filters the news Feed for the 'Me' optionpublic 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 Listif (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 Listreturn contactList;}//filters the news Feed for the 'Me' optionpublic 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 Listif (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 Listreturn contactList;}//Getting All Contactspublic List<FeedsModel> getAllContacts() {List<FeedsModel> contactList = new ArrayList<FeedsModel>();// Select All queryString selectquery = "SELECT * FROM " + table_posts;sqliteDatabase db = this.getWritableDatabase();Cursor cursor = db.rawquery(selectquery, null);// looPing through all rows and adding to Listif (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 Listreturn contactList;}//Getting contacts Countpublic int getContactsCount() {String countquery = "SELECT * FROM " + table_posts;sqliteDatabase db = this.getReadableDatabase();Cursor cursor = db.rawquery(countquery, null);cursor.close();// return countreturn cursor.getCount();}//Updating single contactpublic 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 rowreturn db.update(table_posts, values, ColUMN_ID + " = ?", new String[] { String.valueOf(contact.getID()) });}//Deleting single contactpublic voID deleteContact(FeedsModel contact) {sqliteDatabase db = this.getWritableDatabase();db.delete(table_posts, ColUMN_ID + " = ?", new String[] { String.valueOf(contact.getID()) });db.close();}}
Logcat:
E/CursorWindow(841): Failed to read row 0, column 9 from a CursorWindow which has 3 rows, 9 columns.D/AndroIDRuntime(841): Shutting down VMW/dalvikvm(841): threadID=1: thread exiting with uncaught exception (group=0x40a71930)E/AndroIDRuntime(841): FATAL EXCEPTION: mainE/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.access0(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
解决方法:
这是因为您在检查数据是否可用之前尝试获取数据.
if (cursor.movetoFirst()) { do { // your content } while (cursor.movetoNext());}
将此块更改为
while (cursor.movetoNext()) { // your content}
像这样.
当然可以,对您有帮助.
总结以上是内存溢出为你收集整理的java-无法从CursorWindow读取第0行,第9列全部内容,希望文章能够帮你解决java-无法从CursorWindow读取第0行,第9列所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)