Android 3.0 无法从光标窗口读取行号、列号

发布于 2024-11-25 12:20:03 字数 1673 浏览 0 评论 0原文

我有一个在 android 2.1 上运行良好的应用程序,但是当尝试将其转换到 3.0 时,我收到一个我不熟悉的光标错误。

Java.lang.IllegalStateException: 无法读取 row0、column -1 光标窗口。确保光标之前已正确初始化 从中访问数据。

所有数据都存储在 SQLite 数据库中,此代码在 android 2.1 中运行良好。在 android 3.0 中光标是否必须以不同的方式初始化?

下面列出的是我的代码。

private void OpenGroupData(){
SQLiteDatabase db = openOrCreateDatabase(DATABASE_NAME,Context.MODE_PRIVATE,null);
Cursor cur = db.rawQuery("SELECT groupid FROM properties GROUP BY GroupID" + ";" , null);
LinearLayout glayout = (LinearLayout) findViewById(R.id.Grouplayout);
LinearLayout gwindow = (LinearLayout) findViewById(R.id.groupwindow);

TextView data = new TextView(this);
glayout.addView(data);
data.setText("");
int ID = cur.getColumnIndex("groupid");
int idvalue;

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);

try{
    // Check if our result was valid.
    cur.moveToFirst();
    if (cur != null) {

        // Loop through all Results
        do {data = new TextView(this);
            data.setTextSize(20);
        data.setClickable(true);
        data.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
              GroupClick(v);
            }
          });
        glayout.addView(data);
        idvalue = cur.getInt(ID);
        data.setId(idvalue);
        data.setText("Group: " + idvalue);
        }while(cur.moveToNext());
        } 
        cur.close();
        db.close();
        }   catch(Exception e) {
            Toast.makeText(getApplicationContext(), "Open Group Exception: " + e.toString(), Toast.LENGTH_SHORT).show();
        }
 }

I have an application that runs fine on android 2.1, but when trying to transition it to 3.0 I get a cursor error that I'm not familar with.

Java.lang.IllegalStateException: Couldn't read row0, column -1 from
cursor window. Make sure cursor is initialized correctly before
accessing data from it.

All the data is storred in a SQLite database and this code works fine in android 2.1. Does a cursor have to be initialized differently in android 3.0?

Listed below is my code.

private void OpenGroupData(){
SQLiteDatabase db = openOrCreateDatabase(DATABASE_NAME,Context.MODE_PRIVATE,null);
Cursor cur = db.rawQuery("SELECT groupid FROM properties GROUP BY GroupID" + ";" , null);
LinearLayout glayout = (LinearLayout) findViewById(R.id.Grouplayout);
LinearLayout gwindow = (LinearLayout) findViewById(R.id.groupwindow);

TextView data = new TextView(this);
glayout.addView(data);
data.setText("");
int ID = cur.getColumnIndex("groupid");
int idvalue;

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);

try{
    // Check if our result was valid.
    cur.moveToFirst();
    if (cur != null) {

        // Loop through all Results
        do {data = new TextView(this);
            data.setTextSize(20);
        data.setClickable(true);
        data.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
              GroupClick(v);
            }
          });
        glayout.addView(data);
        idvalue = cur.getInt(ID);
        data.setId(idvalue);
        data.setText("Group: " + idvalue);
        }while(cur.moveToNext());
        } 
        cur.close();
        db.close();
        }   catch(Exception e) {
            Toast.makeText(getApplicationContext(), "Open Group Exception: " + e.toString(), Toast.LENGTH_SHORT).show();
        }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无声无音无过去 2024-12-02 12:20:03

今天早些时候我遇到了同样的错误消息。事实证明,我在列名中输入了错误。因此,如果它仍然适用,您可以去检查列名称是否有拼写错误。请注意,它也区分大小写。对我来说,错误是这样的:

//Trew error
c.getColumnIndex("ArticleNumber");

//Was correct
c.getColumnIndex("Articlenumber");

I ran into the same error message earlier this day. All it turned out to was that I made a typo in the column name. So, if it still applies, you could go and check the column name for typo's. Note that its case sensitive aswell. Error for me was along the lines of:

//Trew error
c.getColumnIndex("ArticleNumber");

//Was correct
c.getColumnIndex("Articlenumber");
-黛色若梦 2024-12-02 12:20:03

好吧,我想通了。由于某种原因,当我尝试将我的应用程序转换到 3.0 时,当我的光标移动并获取字段的列索引时,在本例中(“groupid”)它返回值 -1。当游标尝试从 -1 开始时,它会崩溃,因为它在行(0)、列(-1)处找不到记录。所以我的解决方法是在获取 id 时仅向列索引添加 1。见下文。

 int ID = cur.getColumnIndex("groupid") + 1;
 int idvalue;

通过将 1 添加到列索引,似乎已经解决了问题。

Alright I figured it out. For some reason when trying to transistion my application to 3.0 when my cursor goes and gets the column index for a field, in this case ("groupid") it is returning a value of -1. When the Cursor tries to start at -1 it crashes because it can't find a record at row(0), column(-1). So my fix was to just add one to the column index when getting the id. see below.

 int ID = cur.getColumnIndex("groupid") + 1;
 int idvalue;

By adding 1 to the Column index it seems to have solved the problem.

染柒℉ 2024-12-02 12:20:03

如果 getColumnIndex 返回 -1,则该列不存在。
否则它返回从零开始的列索引。

If getColumnIndex returns -1, then the column doesn't exist.
Otherwize it returnes zero-based column index.

只为守护你 2024-12-02 12:20:03

-1 是每个 sqlite 表都应具有的 _id 列,这是 Android 框架所要求的。如果你必须在索引中添加+1,那么你就在某个地方出错了。

-1 is the _id column which every sqlite table should have as is required by the android framework. If you have to add +1 to the index you are going wrong somewhere.

丶情人眼里出诗心の 2024-12-02 12:20:03

如果找不到“columnName”,则从 getColumnIndex(columnName) 返回 -1 值。检查列名称

The -1 value returns form getColumnIndex(columnName) if 'columnName' can't be found. Check the column names

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文