0

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

每当我在设备上调用 viewEntries() 时,我的应用程序强制关闭并给出 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)
4

1 回答 1

0

尝试清除以前安装的数据:

Settings--> Apps--> [app name]-->Clear data

于 2012-08-01T03:04:57.487 回答