我正在尝试接受用户输入,更正并显示它。当我尝试从存储在数组列表中的单词中更正单词时,发生了我的错误。我不确定为什么会发生此错误。预先感谢您提供的任何帮助或建议。
final String userInput = editText.getText().toString();
userInput.split(" ");
final ArrayList<String> userInputParts = new ArrayList<>();
if (userInput.length() >= 3) {
for (int i = 0; i <= userInput.length(); i++) {
userInputParts.add(userInput);
}
}
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editText == null){}
Toast.makeText(getApplicationContext(),
editText.getText().toString(), Toast.LENGTH_SHORT).show();
for (int i = 0; i <= userInput.length(); i++) {
mScs.getSuggestions(new TextInfo(userInputParts.get(i)), 1);
}
}
});
这些是我遇到的错误,我不太明白它们的含义。我用Google搜索它,它表示我的数组为空,但是我一直在键入单词,然后按按钮,所以我不明白为什么或如何将其保持为空?
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.textwatcher, PID: 15576
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.get(ArrayList.java:437)
at
com.example.textwatcher.MainActivity$1.onClick(MainActivity.java:75)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run
(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)