CursorIndexOutOfBoundsException:请求索引1,大小为1

时间:2015-05-30 23:30:44

标签: android cursor

我在调试器中收到以下警告。我的代码由用户和他们的朋友组成。在FriendsDAO的某个时刻,用户的用户名被引入。但是现在我遇到了下面的游标问题。任何帮助表示赞赏。

05-30 10:32:15.098    1957-1957/com.example.mms.mobile E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.mms.mobile, PID: 1957
android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
        at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
        at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
        at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74)
        at com.example.mms.mobile.UserDAO.cursorToUser(UserDAO.java:182)
        at com.example.mms.mobile.UserDAO.getUser(UserDAO.java:113)
        at com.example.mms.mobile.FriendsDAO.cursorToFriends(FriendsDAO.java:156)
        at com.example.mms.mobile.FriendsDAO.createFriend(FriendsDAO.java:73)
        at com.example.mms.mobile.AddFriendActivity.onClick(AddFriendActivity.java:120)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我的UserDAO和FriendsDAO代码如下:

//UserDAO
public class UserDAO {

public static final String TAG = "UserDAO";

// Database fields
private SQLiteDatabase Database;
private DBHelper DbHelper;
private Context Context;
private String[] AllColumns = {
        DBHelper.USER_ID,
        DBHelper.USER_NAME ,
        DBHelper.PASSWORD ,
        DBHelper.COUNTY ,
        DBHelper.EMAIL ,
        DBHelper.COUNTRY
        };

public UserDAO(Context context) {
    this.Context = context;
    DbHelper = new DBHelper(context);
    // open the database
    try {
        open();
    } catch (SQLException e) {
        Log.e(TAG, "SQLException on opening database " + e.getMessage());
        e.printStackTrace();
    }
}

public void open() throws SQLException {
    Database = DbHelper.getWritableDatabase();
}

public void close() {
    DbHelper.close();
}

public USER createUser(String usrname, String pass, String county, String email,
                       String country) {
    ContentValues values = new ContentValues();
    values.put(DBHelper.USER_NAME ,usrname);
    values.put(DBHelper.PASSWORD ,pass);
    values.put(DBHelper.COUNTY ,county);
    values.put(DBHelper.EMAIL ,email);
    values.put(DBHelper.COUNTRY,country);


    long insertId = Database
            .insert(DBHelper.TABLE_LOGIN, null, values);
    Cursor cursor = Database.query(DBHelper.TABLE_LOGIN, AllColumns,
            DBHelper.USER_ID + " = " + insertId, null, null,null,null);
    cursor.moveToFirst();
    USER newUser = cursorToUser(cursor);
    cursor.close();
    return newUser;
}

public USER getUser(String name) {
    Cursor cursor = Database.query(DBHelper.TABLE_LOGIN, AllColumns,
            DBHelper.USER_NAME + " = ?",
            new String[] { String.valueOf(name) }, null, null, null);
    //if (cursor != null) {
    cursor.moveToFirst();
        while (!cursor.isAfterLast()) {

            USER user = cursorToUser(cursor);

            cursor.moveToNext();

        }

    cursor.close();
    return user;

}

protected USER cursorToUser(Cursor cursor) {
    USER user = new USER();
    user.setId(cursor.getLong(0));
    user.setUser(cursor.getString(1));
    user.setPass(cursor.getString(2));
    user.setCounty(cursor.getString(3));
    user.setEmail(cursor.getString(4));
    user.setCountry(cursor.getString(5));

    return user;
}

}

和FriendsDAO:

public class FriendDAO {

public static final String TAG = "FriendDAO";

// Database fields
private SQLiteDatabase pDatabase;
private DBHelper pDbHelper;
private Context pContext;
private String[] pAllColumns = {
        DBHelper.FRIEND_ID,
        DBHelper.FRIEND_USERNAME,
        DBHelper.list1_ID,
        DBHelper.list2_ID,
        DBHelper.list3_ID,
        DBHelper.location,
        DBHelper.FRIEND_NAME,
        DBHelper.FRIEND_ADDRESS,
        DBHelper.FRIEND_PHONE_NUMBER
        };

public FriendDAO(Context context) {
    this.pContext = context;
    pDbHelper = new DBHelper(context);
    // open the database
    try {
        open();
    } catch (SQLException e) {
        Log.e(TAG, "SQLException on opening database " + e.getMessage());
        e.printStackTrace();
    }
}

public void open() throws SQLException {
    pDatabase = pDbHelper.getWritableDatabase();
}

public void close() {
    pDbHelper.close();
}

public Friend createFriend(String userN, Integer list1ID, Integer list2ID, Integer list3ID,
        String loc, String name, String address, String phone) {
    ContentValues values = new ContentValues();
    values.put(DBHelper.FRIEND_USERNAME, userN);
    values.put(DBHelper.list1_ID, list1ID);
    values.put(DBHelper.list2_ID, list2ID);
    values.put(DBHelper.list3_ID, list3ID);
    values.put(DBHelper.location, loc);
    values.put(DBHelper.FRIEND_NAME, name);
    values.put(DBHelper.FRIEND_ADDRESS, address);
    values.put(DBHelper.FRIEND_PHONE_NUMBER, phone);


    long insertId = pDatabase
            .insert(DBHelper.TABLE_FRIENDS, null, values);
    Cursor cursor = pDatabase.query(DBHelper.TABLE_FRIENDS, pAllColumns,
            DBHelper.FRIEND_ID + " = " + insertId, null, null,null,null);
    cursor.moveToFirst();
    Friend newFriend = cursorToFriend(cursor);
    cursor.close();
    return newFriend;
}

protected Friend cursorToFriend(Cursor cursor) {

    Friend friend = new Friend();
    friend.setId(cursor.getLong(0));
    // get The user by id

    String userN = cursor.getString(1);
    UserDAO dao = new UserDAO(pContext);
    USER user = dao.getUser(userN);
    if(user != null)
        friend.setUser(user);
    friend.setlocation(cursor.getString(2));
    friend.setlist1(cursor.getString(3));
    friend.setlist2Id(cursor.getString(4));
    friend.setlist3Id(cursor.getString(5));
    friend.setName(cursor.getString(6));
    friend.setAddress(cursor.getString(7));
    friend.setPhoneNumber(cursor.getString(8));

    return friend;
}

}

0 个答案:

没有答案