Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 游标索引越界错误_Android_Indexoutofboundsexception_Android Cursoradapter - Fatal编程技术网

Android 游标索引越界错误

Android 游标索引越界错误,android,indexoutofboundsexception,android-cursoradapter,Android,Indexoutofboundsexception,Android Cursoradapter,我正在试验数据库,这里我没有使用helper类,而是在活动中添加了db函数。尝试从获取的行中选择列时,出现索引越界错误 具体地 android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为1错误 main活动 public class MainActivity extends AppCompatActivity { SQLiteDatabase db; private EditText foldernameEditText; priva

我正在试验数据库,这里我没有使用helper类,而是在活动中添加了db函数。尝试从获取的行中选择列时,出现索引越界错误

具体地

android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为1
错误

main活动

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);
        }
    });
}
public类MainActivity扩展了AppCompatActivity{
sqlitedb数据库;
私有EditText foldernameEditText;
私有ListView folderlistview;
私有文本视图mEmptyStateTextView;
ListViewCursorAdapter ListViewCursorAdapter;
最后一个字符串mfoldertable=“foldertable”;
最终字符串mtitletable=“titletable”;
文本视图传输;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=openOrCreateDatabase(“mainDB”,Context.MODE_PRIVATE,null);
db.execSQL(“如果不存在创建表”+mfoldertable+”(_idinteger主键自动递增,文件夹文本整理NOCASE,唯一(文件夹)));
db.execSQL(“如果不存在创建表”+mtitletable+”(_idinteger主键自动递增,id1整数,标题文本,外键(id1)引用“+mfoldertable+”(_id)));
//execSQL(“Drop table”+mfoldertable+”);
//execSQL(“Drop table”+mtitletable+”);
游标c=db.rawQuery(“从“+mfoldertable+”中选择*项,null);
listViewCursorAdapter=新的listViewCursorAdapter(this,c);
folderlistview=(ListView)findViewById(R.id.folderlistview);
mEmptyStateTextView=(TextView)findViewById(R.id.empty\u视图);
setAdapter(listViewCursorAdapter);
setEmptyView(mEmptyStateTextView);
foldernameEditText=(EditText)findViewById(R.id.foldername);
按钮enterFolderButton=(按钮)findViewById(R.id.enterFolderButton);
enterFolderButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
试一试{
execSQL(“插入“+mfoldertable+”(文件夹)值(“+foldernameEditText.getText().toString()+”)”;
}捕获(SQLiteConstraintException异常){
Toast.makeText(getApplicationContext(),“此文件夹已存在”,Toast.LENGTH\u SHORT.show();
}
foldernameEditText.setText(“”);
游标d=db.rawQuery(“从“+mfoldertable+”中选择*,null);
listViewCursorAdapter.swapCursor(d);
}
});
folderlistview.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
意向意向=新意向(MainActivity.this,TitleList.class);
Bundle=新Bundle();
transfer=(TextView)view.findViewById(R.id.list\u item\u folder\u text\u view);
String stuff=transfer.getText().toString();
bundle.putString(“你好”,东西);
游标f=db.rawQuery(“从“+mfoldertable+”中选择*,其中文件夹=”+stuff+”,null);
//Log.e(“helloerror”,f.getString(f.getColumnIndexOrThrow(“u id”));
bundle.putInt(“int”,f.getInt(f.getColumnIndexOrThrow(“u id”));
//bundle.putInt(“int”,1);
意向。额外支出(捆绑);
星触觉(意向);
}
});
}
}

光标f是指向错误的线。不知道为什么。虽然auto increment变量正确显示在输入文件夹旁边的相应文本视图中,但我无法获取它

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")));
}
有关更多帮助,请参阅以下答案: