Android 3.0 Failed to read row #, column # from cursor window

I have an application that works fine on Android 2.1, but when I try to transfer it to 3.0, I get an error with a cursor that I am not familiar with.

Java.lang.IllegalStateException: Failed to read row0, column -1 from cursor window. Make sure that the cursor is initialized correctly accessing data from it.

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

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(); } } 
+6
source share
5 answers

On the same day, I came across the same error message. It turned out that I made a typo in the column name. So, if it is still applicable, you can go and check the column name for typo. Please note that its case is also case sensitive. The error for me was in the lines:

 //Trew error c.getColumnIndex("ArticleNumber"); //Was correct c.getColumnIndex("Articlenumber"); 
+2
source

Ok, I figured it out. For some reason, when I try to switch my application to 3.0, when my cursor goes and gets the column index for the field, in this case ("groupid") it returns -1. When the cursor tries to start at -1, it crashes because it cannot find the record in row (0), column (-1). So my fix was to just add it 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 he solved the problem.

+1
source

If getColumnIndex returns -1, then the column does not exist. Otherwize, it returns the index of the column with a null value.

+1
source

-1 is the _id column that each sqlite table should have, as required by the android framework. If you need to add +1 to the index, you are mistaken somewhere.

+1
source

A value of -1 returns the form getColumnIndex (columnName) if "columnName" is not found. Check column names

0
source

Source: https://habr.com/ru/post/893310/


All Articles