2013-02-11 76 views
0

對不起,重複Question.but我試圖使很多方式不起作用 在applecation中,在第一輪,它可以推動通過活動。但那時那是一個錯誤。IllegalStateException光標錯誤

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)); 

感謝您的指導。

回答

0

檢查的

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

你現在忽略,並告訴你的舉動是否成功的返回值。

+0

THK的答案,但我不明白這一點。當我點擊列表 爲什麼在第一輪過去這個活動,當我回到這個活動是不是過去 – LiNgKung 2013-02-11 19:25:55

+0

我嘗試它,但沒有工作。它沒有顯示消息 「無法移動到第一個」 – LiNgKung 2013-02-11 20:00:34

+0

相同與mCursor.moveToPosition(ARG2)和任何其他使用 – 2013-02-11 20:07:07

0

修正:

  1. 嘗試從它讀取數據之前由moveToFirst定位光標。

  2. 關閉光標後如果(c.moveToFirst()){}

  3. 支票null.e,克;如果(C =空& & c.moveToFirst()!){}

  4. 支票count.e,克; (C!= NULL & & c.getCount()> 0 & & c.moveToFirst()){}