IllegalStateException游标错误

时间:2013-02-11 19:09:44

标签: android sql exception cursor

对不起重复问题。但我试着让很多方法都行不通 在applecation中,在第一轮中,它可以推动Activity。但当时那是一个错误。

SQLiteDatabase mDb;  
Database3 mHelper;  
SQLiteDatabase mDb2;  
Database mHelper2; 

private Cursor mCursor; 
private String name;
private String image;
private String location;
private String info;
private String shop;
private String mcontact;

mHelper2 = new Database(this,shop);  
    mDb2 = mHelper2.getWritableDatabase();  
    mHelper2.onUpgrade(mDb2, 1, 1); 

    if (info.equalsIgnoreCase("null")){
        AlertDialog.Builder builder = new AlertDialog.Builder(Activity_Mall_Floor.this);
        builder.setTitle("Floor Description");
        builder.setMessage("No Floor information of "+name);
        builder.setNeutralButton("OK", null);
        builder.show();

    }
    else {
        ListView listView1 = (ListView)findViewById(R.id.listView2);  
            mHelper = new Database3(this,info);  
            mDb = mHelper.getWritableDatabase();  
            mHelper.onUpgrade(mDb, 1, 1);  

            mCursor = mDb.rawQuery("SELECT " + Database3.COL_FLOOR + ", "   
                    + Database3.COL_IMAGE+ " FROM " + Database3.TABLE_NAME, null);  

            ArrayList<String> dirArray = new ArrayList<String>();  
            mCursor.moveToFirst();  

            while ( !mCursor.isAfterLast() ){  
                dirArray.add(mCursor.getString
                        (mCursor.getColumnIndex(Database3.COL_FLOOR)));   
                mCursor.moveToNext();      
            }  

            ArrayAdapter<String> adapterDir = 
                    new ArrayAdapter<String>(getApplicationContext()   
                    , android.R.layout.simple_list_item_1, dirArray);  
            listView1.setAdapter(adapterDir);
            listView1.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                mCursor.moveToPosition(arg2);

                String floorName = mCursor.getString(mCursor.getColumnIndex(Database3.COL_FLOOR));
                String mapFloor = mCursor.getString(mCursor.getColumnIndex(Database3.COL_IMAGE));

                String floor = "";
                if(floorName.equalsIgnoreCase("1st Floor"))
                    floor = "1";
                else if (floorName.equalsIgnoreCase("2nd Floor"))
                    floor = "2";
                else if (floorName.equalsIgnoreCase("3rd Floor"))
                    floor = "3";
                else
                    floor = "4";


                mCursor = mDb2.rawQuery("SELECT DISTINCT " + Database.COL_NAME 
                        +" FROM " + Database.TABLE_NAME 
                        +" WHERE " + Database.COL_FLOOR + " = '" + floor +"' ", null);

                ArrayList<String> dirArray = new ArrayList<String>();  
                mCursor.moveToFirst();
                while ( !mCursor.isAfterLast() ){  
                    dirArray.add(mCursor.getString
                            (mCursor.getColumnIndex(Database.COL_NAME)));   
                    mCursor.moveToNext();      
                }

                Intent intent = new Intent(getApplicationContext(), Activity_Mall_FMap.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("Floorname", floorName);
                    bundle.putString("Mapfloor", mapFloor);
                    bundle.putString("Shop", shop);
                    intent.putExtra("FloorList",dirArray);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
    }

logcat的错误

02-12 01:31:46.917: E/AndroidRuntime(11370): FATAL EXCEPTION: main
02-12 01:31:46.917: E/AndroidRuntime(11370): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
02-12 01:31:46.917: E/AndroidRuntime(11370):    at android.database.CursorWindow.nativeGetString(Native Method)
02-12 01:31:46.917: E/AndroidRuntime(11370):    at android.database.CursorWindow.getString(CursorWindow.java:442)
02-12 01:31:46.917: E/AndroidRuntime(11370):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
02-12 01:31:46.917: E/AndroidRuntime(11370):    at com.test.mall.Activity_Mall_Floor$1.onItemClick(Activity_Mall_Floor.java:85)
02-12 01:31:46.917: E/AndroidRuntime(11370):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
02-12 01:31:46.917: E/AndroidRuntime(11370):    at android.widget.AbsListView.performItemClick(AbsListView.java:1068)
02-12 01:31:46.917: E/AndroidRuntime(11370):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2525)

这是错误行85

String floorName = mCursor.getString(mCursor.getColumnIndex(Database3.COL_FLOOR));
String mapFloor = mCursor.getString(mCursor.getColumnIndex(Database3.COL_IMAGE));

感谢您的指导。

2 个答案:

答案 0 :(得分:0)

检查

的返回值
if (!mCursor.moveToFirst()) {
   Log.e(TAG, "Couldn't move to first");
   // do something here if move failed 
}

你现在忽略它并告诉你移动是否成功。

答案 1 :(得分:0)

<强> 修正:

  1. 尝试通过moveToFirst定位光标,然后再从中读取数据。

  2. if(c.moveToFirst()){}

  3. 后关闭光标
  4. 检查null.e,g; if(c!= null&amp;&amp; c.moveToFirst()){}

  5. 检查count.e,g; (c!= null&amp;&amp; c.getCount()&gt; 0&amp;&amp; c.moveToFirst()){}