Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 3.0 Couldn't read row#, column# from cursor window

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();
        }
 }
like image 527
Jonathan Avatar asked Jul 21 '11 19:07

Jonathan


1 Answers

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");
like image 158
Bart Avatar answered Sep 27 '22 23:09

Bart