由 java.lang.IndexOutOfBoundsException 引起:索引:2,大小:2 由 SoftKeyboard
Caused by java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 by SoftKeyboard
嘿,我正在努力检测我的应用程序中的自定义键盘。我成功地做到了这一点,并使用代码
检测到用户是自定义键盘
fun isUsingCustomKeyboard(context: Context): Boolean {
val imm: InputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val inputMethodProperties: List<InputMethodInfo> = imm.enabledInputMethodList
for (i in 0..inputMethodProperties.size) { // loop to every input method
val inputMethodInfo = inputMethodProperties[i]
val inputMethodId = Settings.Secure.getString(
context.contentResolver,
Settings.Secure.DEFAULT_INPUT_METHOD
)
if (inputMethodInfo.id.equals(inputMethodId)) {
if (inputMethodInfo.serviceInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM == 0) {
return true
}
break
}
}
return false
}
但是我收到了这个错误,有人可以告诉我上面的代码出了什么问题吗
com.example.android.KeyboardHelper.isUsingCustomKeyboard (KeyboardHelper.kt:15)
com.example.android.trackKeyboardType (SignInActivity.kt:75)
com.example.android.onCreate (SignInActivity.kt:71)
android.app.Activity.performCreate (Activity.java:8207)
android.app.Activity.performCreate (Activity.java:8191)
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1309)
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3808)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:4011)
android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:85)
android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:2325)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loop (Looper.java:246)
android.app.ActivityThread.main (ActivityThread.java:8633)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)
for (i in 0..inputMethodProperties.size) {
大小给出了列表中的对象数,但索引从 0 开始,因此当您尝试读取最后一个时,它超出了范围。大小 -1 是最后一个索引。
嘿,我正在努力检测我的应用程序中的自定义键盘。我成功地做到了这一点,并使用代码
检测到用户是自定义键盘fun isUsingCustomKeyboard(context: Context): Boolean {
val imm: InputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val inputMethodProperties: List<InputMethodInfo> = imm.enabledInputMethodList
for (i in 0..inputMethodProperties.size) { // loop to every input method
val inputMethodInfo = inputMethodProperties[i]
val inputMethodId = Settings.Secure.getString(
context.contentResolver,
Settings.Secure.DEFAULT_INPUT_METHOD
)
if (inputMethodInfo.id.equals(inputMethodId)) {
if (inputMethodInfo.serviceInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM == 0) {
return true
}
break
}
}
return false
}
但是我收到了这个错误,有人可以告诉我上面的代码出了什么问题吗
com.example.android.KeyboardHelper.isUsingCustomKeyboard (KeyboardHelper.kt:15)
com.example.android.trackKeyboardType (SignInActivity.kt:75)
com.example.android.onCreate (SignInActivity.kt:71)
android.app.Activity.performCreate (Activity.java:8207)
android.app.Activity.performCreate (Activity.java:8191)
android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1309)
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3808)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:4011)
android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:85)
android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:2325)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loop (Looper.java:246)
android.app.ActivityThread.main (ActivityThread.java:8633)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)
for (i in 0..inputMethodProperties.size) {
大小给出了列表中的对象数,但索引从 0 开始,因此当您尝试读取最后一个时,它超出了范围。大小 -1 是最后一个索引。