随机Android Sqlite崩溃

时间:2016-04-13 05:02:28

标签: java android sqlite

我不确定为什么会收到此错误:

java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

我在sqlite中获取这些行:

db_d.setIsFollowingType(c.getInt(c.getColumnIndex(KEY_IS_FOLLOWING_TYPE)));
db_d.setIsFollowerType(c.getInt(c.getColumnIndex(KEY_IS_FOLLOWER_TYPE)));

如果我评论它们有效,这就是整个sqlite方法:

public List<DatabaseUser> getUsers(int type) {

        String whereClause = KEY_IS_FOLLOWING_TYPE + " = ? ";
        if(type == 1) {

            whereClause = KEY_IS_FOLLOWER_TYPE + " = ? ";
        }
        List<DatabaseUser> getUsers = new ArrayList<DatabaseUser>();
        SQLiteDatabase db = this.getReadableDatabase();
        String[] whereArgs = new String[]{
                "1"
        };
        String orderBy = KEY_ID + " DESC";

        Cursor c = db.query(TABLE_USERS, null, whereClause, whereArgs, null, null, orderBy);

        if (c.moveToFirst()) {
            do {
                DatabaseUser db_d = new DatabaseUser();

                db_d.setUserId(c.getString(c.getColumnIndex(KEY_USER_ID)));
                db_d.setPicture(c.getString(c.getColumnIndex(KEY_PICTURE)));
                db_d.setUsername(c.getString(c.getColumnIndex(KEY_USERNAME)));

                db_d.setFollowersCount(c.getInt(c.getColumnIndex(KEY_FOLLOWERS_COUNT)));
                db_d.setFollowingCount(c.getInt(c.getColumnIndex(KEY_FOLLOWING_COUNT)));
                db_d.setTrackingCount(c.getInt(c.getColumnIndex(KEY_TRACKING_COUNT)));
                db_d.setIsFollowingType(c.getInt(c.getColumnIndex(KEY_IS_FOLLOWING_TYPE)));
                db_d.setIsFollowerType(c.getInt(c.getColumnIndex(KEY_IS_FOLLOWER_TYPE)));
                db_d.setIsChanged(c.getInt(c.getColumnIndex(KEY_IS_CHANGED)));

                db_d.setDateAdded(c.getLong(c.getColumnIndex(KEY_DATE_ADDED)));

                Log.d(Constants.DEBUG, "DB pull user product " + db_d);
                getUsers.add(db_d);
            } while (c.moveToNext());
        }

        //calling for close for recycling make sure it does not cause an error
        c.close();
        return getUsers;

    }

这是我的表创建方法:

private static final String CREATE_TABLE_USERS = "CREATE TABLE "
            + TABLE_USERS + "("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + KEY_USER_ID + " TEXT NOT NULL UNIQUE ON CONFLICT REPLACE,"
            + KEY_PICTURE + " TEXT,"
            + KEY_USERNAME + " TEXT,"
            + KEY_FOLLOWERS_COUNT + " INTEGER,"
            + KEY_FOLLOWING_COUNT + " INTEGER,"
            + KEY_TRACKING_COUNT + " INTEGER,"
            + KEY_IS_FOLLOWING_TYPE + " INTEGER,"
            + KEY_IS_FOLLOWER_TYPE + " INTEGER,"
            + KEY_IS_CHANGED + " INTEGER,"
            + KEY_DATE_ADDED + " LONG"
            + ")";

这是DatabaseUser对象:

public class DatabaseUser {



    private String userId, picture, username;
    private int followersCount, followingCount, trackCount, isChanged, isFollowingType, isFollowerType; 
    private long dateAdded;


    //TODO USER NO NEED FOR ISFOLLOWING
    public DatabaseUser() {
    }


    //TODO TESTING
    public DatabaseUser(String uId, String pic, String uname,
                        int followersC, int followingC, int trackC, int isChng, int followingtype, int followertype,
                        long dateAdd) {
        userId = uId;
        picture = pic;
        username = uname;
        followersCount = followersC;
        followingCount = followingC;
        trackCount = trackC;
        isChanged = isChng;
        isFollowingType = followingtype;
        isFollowerType = followertype;
        dateAdded = dateAdd;


    }


    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getFollowersCount() {
        return followersCount;
    }

    public void setFollowersCount(int followersCount) {
        this.followersCount = followersCount;
    }

    public int getFollowingCount() {
        return followingCount;
    }

    public void setFollowingCount(int followingCount) {
        this.followingCount = followingCount;
    }

    public int getTrackingCount() {
        return trackCount;
    }

    public void setTrackingCount(int trackCount) {
        this.trackCount = trackCount;
    }

    public int getIsChanged() {
        return isChanged;
    }

    public void setIsChanged(int isChanged) {
        this.isChanged = isChanged;
    }

    public int getIsFollowingType() {
        return isFollowingType;
    }

    public void setIsFollowingType(int isFollowingType) {
        this.isFollowingType = isFollowingType;
    }

    public long getDateAdded() {
        return dateAdded;
    }

    public void setDateAdded(long dateAdded) {
        this.dateAdded = dateAdded;
    }

    public int getIsFollowerType() {
        return isFollowerType;
    }

    public void setIsFollowerType(int isFollowerType) {
        this.isFollowerType = isFollowerType;
    }
}

这是我在MainHome Activity中进行检查的地方:

 private void testCreateUserFollowing() {


        //0 is for isType following
        DatabaseUser testUserFollowing1 = new DatabaseUser("userId1", "pictureId", "username1", 2354, 30534, 498231, 0, 1, 3, 0);
        DatabaseUser testUserFollowing2 = new DatabaseUser("userId2", "pictureId", "username2", 2750000, 30000000, 4, 0, 1, 3, 0);
        DatabaseUser testUserFollowing3 = new DatabaseUser("userId3", "pictureId", "username3", 2, 3, 4, 0, 1, 3, 0);



        DatabaseUser testUserFollower1 = new DatabaseUser("userId4", "pictureId", "username4", 2, 3, 4, 0, 3, 1, 0);
        DatabaseUser testUserFollower2 = new DatabaseUser("userId5", "pictureId", "username5", 2, 3, 4, 0, 3, 1, 0);
        DatabaseUser testUserFollower3 = new DatabaseUser("userId6", "pictureId", "username6", 2, 3, 4, 0, 3, 1, 0);

        DatabaseUser checkUsersExist = db.getSingleUser("userId3");
        if(checkUsersExist != null) {

        } else {
            db.createUser(testUserFollowing1);
            db.createUser(testUserFollowing2);
            db.createUser(testUserFollowing3);
            db.createUser(testUserFollower1);
            db.createUser(testUserFollower2);
            db.createUser(testUserFollower3);
        }


    }

如前所述评论tee两个字段的工作原理,但是我需要它才能解除它的注释...另外,我意识到其他一些sqlite错误会弹出另一行,而另一行表示同样的错误...如果这有助于任何事情。我在包含sqlite方法的SQLITE Helper类上没有错误......所以不确定为什么会出现这个错误......

更新:对任何感兴趣的人: 显然是在调用deleteDatabase(&#34; dbname&#34;);只有工作,卸载或清除应用程序的数据不...

1 个答案:

答案 0 :(得分:-1)

首先,您需要检查游标是否为null然后从中获取数据后,如下面的

if(c!=null){
 if (c.moveToFirst()) {
            do {
                DatabaseUser db_d = new DatabaseUser();

                db_d.setUserId(c.getString(c.getColumnIndex(KEY_USER_ID)));
                db_d.setPicture(c.getString(c.getColumnIndex(KEY_PICTURE)));
                db_d.setUsername(c.getString(c.getColumnIndex(KEY_USERNAME)));

                db_d.setFollowersCount(c.getInt(c.getColumnIndex(KEY_FOLLOWERS_COUNT)));
                db_d.setFollowingCount(c.getInt(c.getColumnIndex(KEY_FOLLOWING_COUNT)));
                db_d.setTrackingCount(c.getInt(c.getColumnIndex(KEY_TRACKING_COUNT)));
                db_d.setIsFollowingType(c.getInt(c.getColumnIndex(KEY_IS_FOLLOWING_TYPE)));
                db_d.setIsFollowerType(c.getInt(c.getColumnIndex(KEY_IS_FOLLOWER_TYPE)));
                db_d.setIsChanged(c.getInt(c.getColumnIndex(KEY_IS_CHANGED)));

                db_d.setDateAdded(c.getLong(c.getColumnIndex(KEY_DATE_ADDED)));

                Log.d(Constants.DEBUG, "DB pull user product " + db_d);
                getUsers.add(db_d);
            } while (c.moveToNext());
        }
}