【问题标题】:Cursor index out of bound error光标索引超出范围错误
【发布时间】:2017-06-05 18:49:22
【问题描述】:

我正在试验数据库,在这里我没有使用辅助类,而是在活动中添加了 db 函数。尝试从获得的行中选择一列时出现索引超出范围错误。

具体来说

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1 错误。

MainActivity

public class MainActivity extends AppCompatActivity {

SQLiteDatabase db;
private EditText foldernameEditText;
private ListView folderlistview;
private TextView mEmptyStateTextView;
ListViewCursorAdapter listViewCursorAdapter;
final String mfoldertable = "foldertable";
final String mtitletable = "titletable";
TextView transfer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    db = openOrCreateDatabase("mainDB", Context.MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS " + mfoldertable + "(_id integer primary key autoincrement, folder text collate NOCASE, UNIQUE(folder))");
    db.execSQL("CREATE TABLE IF NOT EXISTS "+mtitletable+ "(_id integer primary key autoincrement,id1 integer, title text, FOREIGN KEY(id1) REFERENCES "+mfoldertable+"(_id))");
    //db.execSQL("Drop table "+mfoldertable+"");
    //db.execSQL("Drop table "+mtitletable+"");
    Cursor c = db.rawQuery("SELECT * FROM " + mfoldertable + "", null);
    listViewCursorAdapter = new ListViewCursorAdapter(this, c);
    folderlistview = (ListView) findViewById(R.id.folderlistview);
    mEmptyStateTextView = (TextView) findViewById(R.id.empty_view);
    folderlistview.setAdapter(listViewCursorAdapter);
    folderlistview.setEmptyView(mEmptyStateTextView);
    foldernameEditText = (EditText) findViewById(R.id.foldername);
    Button enterFolderButton = (Button) findViewById(R.id.enterFolderButton);
    enterFolderButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                db.execSQL("INSERT INTO "+mfoldertable+"(folder) VALUES('"+ foldernameEditText.getText().toString() +"')");
            } catch (SQLiteConstraintException exception) {
                Toast.makeText(getApplicationContext(), "This folder already exists", Toast.LENGTH_SHORT).show();
            }
            foldernameEditText.setText("");
            Cursor d = db.rawQuery("SELECT * FROM " + mfoldertable + "", null);
            listViewCursorAdapter.swapCursor(d);
        }
    });

    folderlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent = new Intent(MainActivity.this, TitleList.class);
            Bundle bundle = new Bundle();
            transfer = (TextView) view.findViewById(R.id.list_item_folder_text_view);
            String stuff = transfer.getText().toString();
            bundle.putString("hello", stuff);
            Cursor f=db.rawQuery("SELECT * FROM "+mfoldertable+" where folder='"+stuff+'"",null);
            //Log.e("helloerror",f.getString(f.getColumnIndexOrThrow("_id")));
            bundle.putInt("int",f.getInt(f.getColumnIndexOrThrow("_id")));

            //bundle.putInt("int",1);
            intent.putExtras(bundle);
            startActivity(intent);
        }
    });
}

}

光标 f 是指向错误的行。想不通为什么。尽管自动增量变量正确显示在输入文件夹旁边的相应文本视图上,但我无法获得它。

【问题讨论】:

    标签: android indexoutofboundsexception android-cursoradapter


    【解决方案1】:

    android.database.CursorIndexOutOfBoundsException

    此异常表示您正在尝试使用无效索引访问游标。确保在获得光标对象后立即调用 moveToFirst() 方法。这将返回一个布尔值,表示您的光标是否有任何数据,如果有任何数据,则将光标设置为第一个元素。

    替换这个:

    bundle.putInt("int",f.getInt(f.getColumnIndexOrThrow("_id")));
    

    有了这个:

    if (f != null && f.moveToFirst()) {
           bundle.putInt("int",f.getInt(f.getColumnIndexOrThrow("_id")));
    }
    

    如需进一步帮助,请参阅此答案:android.database.CursorIndexOutOfBoundsException

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多