2013-07-09 89 views
1

我有一個活動,用戶可以從spinners中填寫。但如果用戶沒有填寫任何這個微調,然後點擊保存按鈕,AlertDialog必須顯示。CursorIndexOutOfBoundsException索引0請求,在Android中的大小爲0

但是,它不是我所期望的。當我點擊保存按鈕,它顯示了一個錯誤:「CursorIndexOutOfBoundsException指數0請求,大小爲0

我怎樣才能解決這種類型的錯誤?

DatabaseHandler.java

public Cursor getReport_DistrictCode(String district) 
{ 
    SQLiteDatabase dbSqlite = this.getReadableDatabase(); 
    Cursor c = dbSqlite.query(Constants.TABLE_DISTRICT, new String[] 
      {Constants.DISTRICT_ID, Constants.DISTRICT_CODE,Constants.DISTRICT_NAME,Constants.DISTRICT_DESCRIPTION}, 
      Constants.DISTRICT_NAME+" = ?" , new String[]{district}, null, null, null); 
    if (c != null) { 
     c.moveToFirst(); 
    } 
    dbSqlite.close(); 
    return c; 
} 

MainActivity.java

spn_District.setOnItemSelectedListener(new OnItemSelectedListener() { 
    @Override 
     public void onItemSelected(AdapterView<?> parentView, View SelectedItem, int position, long id) 
     { 
      int rowid = (int)parentView.getItemIdAtPosition(position); 
      int districtId = rowid; 
      if(districtId == 0) 
      { 
       List<String> Province = new ArrayList<String>(); 
       ArrayAdapter<String> adapter= new ArrayAdapter<String>(S_10th_IReportMain.this, android.R.layout.simple_spinner_item, Province); 
       spn_Province.setAdapter(adapter); 

      } 

      List<String> Province = databaseHandler.setItemOnProvinceSpinner(districtId); 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(S_10th_IReportMain.this, R.layout.spinnerattributecell, Province) { 
       public View getView(int position, View convertView, ViewGroup parent) 
       { 
        View v = super.getView(position, convertView, parent); 

        Typeface externalFont=Typeface.createFromAsset(getAssets(), "Gothic_Regular.TTF"); 
        ((TextView) v).setTypeface(externalFont); 

        return v; 
       } 

       public View getDropDownView(int position, View convertView, ViewGroup parent) { 
         View v =super.getDropDownView(position, convertView, parent); 

        Typeface externalFont=Typeface.createFromAsset(getAssets(), "Gothic_Regular.TTF"); 
        ((TextView) v).setTypeface(externalFont); 

        return v; 
       } 
      }; 
      adapter.setDropDownViewResource(R.layout.spinnerdropdownitem); 
      spn_Province.setAdapter(adapter); 

     } 
     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 

     } 

    }); 

我的CursorAdapter的執行情況MainActivity.java

String District = spn_District.getSelectedItem().toString(); 
Cursor rDistrict = databaseHandler.getReport_DistrictCode(District); 
String DistrictCode = rDistrict.getString(rDistrict.getColumnIndex(Constants.DISTRICT_CODE)); 
+0

你可以給你的執行中的CursorAdapter? –

+0

嗨@KapilVats,這是最後附上的代碼 – androidBoomer

+0

哪條線你得到這個錯誤 –

回答

2

請祛瘀E本從getReport_DistrictCode

if (c != null) { 
     c.moveToFirst(); 
    } 

而是隻返回光標,然後Cursor rDistrict = databaseHandler.getReport_DistrictCode(District);後,而不是做一個空檢查,添加此檢查if(rDistrict.moveToFirst()){ // add your logic}

+0

你能特別在最後一段中給出明確的答案嗎? – androidBoomer

+0

遊標對象永遠不會等於null,但它可以是空的(有0行)。如果你需要檢查,你可以使用if(cursor.getCount()> 0)。你的代碼失敗了,因爲你認爲這是因爲你的空檢查已經到位,光標不會爲空。 –

相關問題