游标与Jellybean不兼容?

时间:2012-08-01 02:02:01

标签: android sqlite cursor

我正在处理一些从数据库中获取名称和编号并显示它的代码。该代码适用于运行1.6的模拟器,但不适用于运行4.1.1的Galaxy Nexus。

每当我在我的设备上调用viewEntries()时,我的app力就会关闭并发出IllegalStateException,表明无法找到该列。 WhitelistDB从SQLiteOpenHelper扩展。

感谢您的帮助。

public String getData()
{
    WhitelistDB DB = new WhitelistDB(this);
    SQLiteDatabase db = DB.getReadableDatabase();

    String[] columns = new String[]{WhitelistDB.KEY_ID, WhitelistDB.KEY_NAME, WhitelistDB.KEY_NUMBER};
    Cursor c = db.query(WhitelistDB.TABLE_NAME, columns, null, null, null, null, null);
    String result = "";

    int count = c.getColumnCount();
    String noEntries = "No entries in whitelist.";

    if(count < 1)
    {
        return noEntries;
    }

    int iRow = c.getColumnIndex(WhitelistDB.KEY_ID);
    int iName = c.getColumnIndex(WhitelistDB.KEY_NAME);
    int iNumber = c.getColumnIndex(WhitelistDB.KEY_NUMBER);

    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
    {
        result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iNumber) + "\n";
    }

    db.close();

    return result;
}

public void viewEntries()
{
    TextView tv = (TextView) findViewById(R.id.wlData);
    String info = getData();
    tv.setText(info);
}

这是我得到的错误。

07-31 18:42:10.706: E/CursorWindow(10024): Failed to read row 0, column -1 from a CursorWindow which has 7 rows, 3 columns.
07-31 18:42:10.706: D/AndroidRuntime(10024): Shutting down VM
07-31 18:42:10.706: W/dalvikvm(10024): threadid=1: thread exiting with uncaught exception (group=0x41dbe300)
07-31 18:42:10.714: E/AndroidRuntime(10024): FATAL EXCEPTION: main
07-31 18:42:10.714: E/AndroidRuntime(10024): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mutoor.autoreply/com.mutoor.autoreply.WhitelistActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
07-31 18:42:10.714: E/AndroidRuntime(10024):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)

1 个答案:

答案 0 :(得分:0)

尝试使用以下方法清除以前安装的数据:

设置 - &gt; Apps - &gt; [app name] - &gt; 清除数据

相关问题