gpt4 book ai didi

java - Kotlin 数据库错误 CursorIndexOutOfBoundsException

转载 作者:搜寻专家 更新时间:2023-11-01 09:32:44 28 4
gpt4 key购买 nike

这是错误,它说索引超出范围,但我不知道如何解决它,有一些土耳其语单词但它们并不重要,我认为:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.burhanozen.nothesaplama, PID: 26919
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.burhanozen.nothesaplama/com.example.burhanozen.nothesaplama.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at ........

我试图存储某种学生信息。我有这些错误,我在下面分享代码。这是我的 MainActivity:

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)
val fab = findViewById(R.id.fab) as FloatingActionButton
fab.setOnClickListener {
val intent = Intent(this@MainActivity,NotEkrani::class.java)
startActivity(intent) }


val studentsArray = ArrayList<String>()

val arrayAdapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,studentsArray)
listView.adapter= arrayAdapter

try{

val myDatabase = this.openOrCreateDatabase("student", Context.MODE_PRIVATE,null)
myDatabase.execSQL("CREATE TABLE IF NOT EXISTS students (isim TEXT," +
" dersbir TEXT, dersbirkredi TEXT, dersbirort TEXT," +
" dersiki TEXT, dersikikredi TEXT, dersikiort TEXT) ")

val cursor = myDatabase.rawQuery("select * from students",null)

val nameIx = cursor.getColumnIndex("isim")
val dersbirIx = cursor.getColumnIndex("dersbir")
val dersbirkrediIx = cursor.getColumnIndex("dersbirkredi")
val dersbirortIx = cursor.getColumnIndex("dersbirort")
val dersikiIx = cursor.getColumnIndex("dersiki")
val dersikikrediIx = cursor.getColumnIndex("dersikikredi")
val dersikiortIx = cursor.getColumnIndex("dersikiort")

cursor.moveToNext()

while (cursor != null){

studentsArray.add(cursor.getString(nameIx))
studentsArray.add(cursor.getString(dersbirIx))
studentsArray.add(cursor.getString(dersbirkrediIx))
studentsArray.add(cursor.getString(dersbirortIx))
studentsArray.add(cursor.getString(dersikiIx))
studentsArray.add(cursor.getString(dersikikrediIx))
studentsArray.add(cursor.getString(dersikiortIx))

cursor.moveToNext()

arrayAdapter.notifyDataSetChanged()


}
while (cursor!=null){
cursor!!.close()
}



}catch (e:SQLException){

}

这是我的第二个 Activity 页面,我在这里没有任何错误,但这可能会有所帮助。我在这里写了数据库代码。

fun kaydet(view:View){
val isim = isim.text.toString()
val dersbir = dersbir.text.toString()
val dersbirkredi = dersbirkredi.text.toString()
val dersbirort = dersbirort.text.toString()
val dersiki = dersiki.text.toString()
val dersikikredi = dersikikredi.text.toString()
val dersikiort = dersikiort.text.toString()

try {
val myDatabase = openOrCreateDatabase("student", Context.MODE_PRIVATE,null)

myDatabase.execSQL("CREATE TABLE IF NOT EXISTS students (isim TEXT," +
" dersbir TEXT, dersbirkredi TEXT, dersbirort TEXT," +
" dersiki TEXT, dersikikredi TEXT, dersikiort TEXT) ")

val sqlString = "INSERT INTO students (isim,dersbir,dersbirkredi,dersbirort,dersiki,dersikikredi,dersikiort) VALUES(?,?,?,?,?,?,?)"
val statement = myDatabase.compileStatement(sqlString)

statement.bindString(1,isim)
statement.bindString(2,dersbir)
statement.bindString(3,dersbirkredi)
statement.bindString(4,dersbirort)
statement.bindString(5,dersiki)
statement.bindString(6,dersikikredi)
statement.bindString(7,dersikiort)
statement.execute()


}catch (e:SQLException){

}
val intent = Intent(this,MainActivity::class.java)
startActivity(intent)

}

最佳答案

将您的 while 循环更改为如下所示:

    while (cursor.moveToNext()){

studentsArray.add(cursor.getString(nameIx))
...


}

(从您的代码中删除所有其他 cursor.moveToNext())

您正在 while 循环条件中检查 cursor != null,但在您从游标读取所有行后它不会变为 null。

在上面的代码中,如果有下一行,cursor.moveToNext 返回 true。否则返回 false 并且循环终止。

而且,刚刚注意到,您应该将其更改为

while (cursor!=null){
cursor!!.close()
}

如果检查:

if (cursor!=null){
cursor.close()
}

或者,作为 Todd评论中建议,您可以使用自动关闭游标的高阶函数use,这类似于 Java 7 try-with-resources。

cursor.use {
while (cursor.moveToNext()){

studentsArray.add(cursor.getString(nameIx))
...


}
}

关于java - Kotlin 数据库错误 CursorIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45784788/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com