我是一名初学者,正在尝试为我的学校项目构建一个健康应用程序
当我使用Android 11(API 30)时,程序运行良好。当我使用高于 11 (API 30+) 的 Android 时发生错误。
发生错误的代码:
private fun refreshMe(){
val helper = profileHelper(applicationContext)
val db = helper.readableDatabase
val rs = db.rawQuery("SELECT * FROM uProfile", null)
rs.moveToLast()
nama.text = rs.getString(1) //THE ERROR HAPPEN HERE
umur.text = rs.getString(2)
jk.text = rs.getString(3)
data.text = rs.getString(4)
kadarAu.text = rs.getString(5)
kadarGd.text = rs.getString(6)
kadarHb.text = rs.getString(7)
kadarCh.text = rs.getString(8)
kadarTdSis.text = rs.getString(9)
kadarTdDia.text = rs.getString(10)
rs.close()
}
错误日志
FATAL EXCEPTION: main
Process: com.lhsproj.labhealthpocket, PID: 10098
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lhsproj.labhealthpocket/com.lhsproj.labhealthpocket.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3782)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8177)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:521)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:139)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:53)
at com.lhsproj.labhealthpocket.MainActivity.refreshMe(MainActivity.kt:63)
at com.lhsproj.labhealthpocket.MainActivity.onCreate(MainActivity.kt:251)
at android.app.Activity.performCreate(Activity.java:8595)
at android.app.Activity.performCreate(Activity.java:8573)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3764)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8177)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
我之前尝试过 Stack Overflow 上的几种解决方案,但我发现它们很难在我的项目中实现。
´´´android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0´´´
您的光标为空。 检查它是否为空。