【问题标题】:CursorIndexOutOfBoundsException Index 0 requested, with a size of 0 in AndroidCursorIndexOutOfBoundsException 请求索引 0,Android 中大小为 0
【发布时间】:2013-07-09 13:06:16
【问题描述】:

我有一个活动,用户可以在其中填写微调器。但是,如果用户没有填写此微调器中的任何内容,然后单击“保存”按钮,则必须显示 AlertDialog。

然而,这并不像我预期的那样。当我单击保存按钮时,它显示错误:“CursorIndexOutOfBoundsException Index 0 requested, with a size of 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));  

【问题讨论】:

  • 你能提供你的 cursoradapter 的实现吗?
  • 嗨@KapilVats,这是最后附上的代码
  • 你在哪一行得到这个错误
  • 它指向这一行:String DistrictCode = rDistrict.getString(rDistrict.getColumnIndex(Constants.DISTRICT_CODE));

标签: android android-cursor


【解决方案1】:

请从您的 getReport_DistrictCode 中删除此内容

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

而不是只返回光标,然后在 Cursor rDistrict = databaseHandler.getReport_DistrictCode(District); 之后,而不是进行空检查,添加此检查 if(rDistrict.moveToFirst()){ // add your logic}

【讨论】:

  • 你能给出一个最明确的答案吗,尤其是在你的最后一段。
  • 游标对象永远不会等于null,但它可以为空(有0行)。如果您需要检查,可以使用 if(cursor.getCount() > 0)。您的代码失败是因为您假设由于您的空检查到位,光标不会为空。
猜你喜欢
  • 2013-01-10
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-02
相关资源
最近更新 更多