0

很抱歉重复了这个问题。但是我尝试让很多方法都不起作用在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));

谢谢指导。

4

2 回答 2

0

检查返回值

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

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

于 2013-02-11T19:14:22.647 回答
0

使固定:

  1. 在从中读取数据之前尝试通过 moveToFirst 定位光标。

  2. 在 if ( c.moveToFirst()) {} 之后关闭光标

  3. 检查 null.e,g; if (c != null && c.moveToFirst()) {}

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

于 2017-04-05T23:41:04.327 回答