试图调用方法我得到 java.lang.IndexOutOfBoundsException

谢尔盖·霍夫

我正在尝试在 android 上编写游戏,文本任务。

我有一个叫做“工作”的过程,它的作用是让玩家有机会选择工作地点和他想要工作的时间,然后处理这些信息。

下面是两个方法“ checkWorkPlaces() ”的代码,它检查城市玩家是否有工作的地方,然后它调用“ chooseWorkPlace() ”方法,让玩家有机会选择他想要工作的地方。

  • tvInfo - TextView,我会显示关于它的所有信息。
  • player - 我的自定义类Player 的对象,具有自定义类Place 的位置等属性,具有属性名称workPlaces - 包含类WorkPlace 的工作地点的数组,其中每个都有定义名称属性。
  • answerContainer - 只是我用来在方法之间传递一些信息的ArrayList
  • btnMainGame - 实际上只是一个Button,我用来保存用户信息
  • hideButtons() - 在屏幕上隐藏一些按钮 [对我的问题不重要]

    protected void checkWorkPlaces() {
    
    answerContainer.clear();
    
    if (player.location.workPlaces.length > 0) {
    
        tvInfo.setText("In " + player.location.name + " there are a few places to work:");
    
        for (WorkPlace workPlace : player.location.workPlaces) {
            tvInfo.append("\n" + workPlace.defenition);
        }
        tvInfo.append("Where do you want to work?");
    
        chooseWorkPlace();
    
    
    } else {
        tvInfo.setText("There is nowhere to work in " + player.location.name);
    }
    
    }
    
    
    protected void chooseWorkPlace() {
    
    hideButtons();
    
    btnMainGame.setText(getString(R.string.save));
    btnMainGame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (etInput.getText().toString().isEmpty()){
                Toast.makeText(MainActivity.this, "Please enter something", Toast.LENGTH_SHORT).show();
                chooseWorkPlace();
    
            } else {
                answerContainer.add(etInput.getText().toString().toLowerCase());
    
            }
        }
    });
    
    String choice = answerContainer.get(0).toString().toLowerCase();
    answerContainer.clear();
    
    if (!Objects.equals(choice, "back")) {
    
        for (WorkPlace workPlace : player.location.workPlaces) {
    
            if (Objects.equals(workPlace.name.toLowerCase(), choice)) {
    
                answerContainer.add(workPlace);
                return;
            }
        }
    
        tvInfo.append("\n\nThere is no such work place as: " + choice + ", try again");
        chooseWorkPlace();
    
    }
    
    }
    

所以,问题是,当程序开始调用chooseWorkPlace()方法时,它会用 java.lang.IndexOutOfBoundsException 停止程序

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.griseostrigiformes.textwarrior, PID: 4916
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.get(ArrayList.java:437)
    at com.example.griseostrigiformes.textwarrior.MainSystem.MainActivity.chooseWorkPlace(MainActivity.java:521)
    at com.example.griseostrigiformes.textwarrior.MainSystem.MainActivity.checkWorkPlaces(MainActivity.java:492)
    at com.example.griseostrigiformes.textwarrior.MainSystem.MainActivity$2.onClick(MainActivity.java:124)
    at android.view.View.performClick(View.java:6256)
    at android.view.View$PerformClick.run(View.java:24701)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

我该如何解决这个问题?

Pushpesh Kumar Rajwanshi

这一行似乎给了你错误,

String choice = answerContainer.get(0).toString().toLowerCase();

可能是您应该answerContainer在获取零索引元素之前先检查的大小或者你应该调试为什么answerContainer是空的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

事件java.lang.IndexOutOfBoundsException的断点

Android服务java.lang.IndexOutOfBoundsException

LastIndexOf和java.lang.IndexOutOfBoundsException

“线程1” java.lang.IndexOutOfBoundsException

java.lang.IndexOutOfBoundsException错误Bukkit插件

java.lang.IndexOutOfBoundsException: 使用 try, catch

Android java.lang.IndexOutOfBoundsException 错误

得到这个错误java.lang.IndexOutOfBoundsException:索引487超出长度487

JsArray <?>的大小为1,但得到“ java.lang.IndexOutOfBoundsException:索引:0,大小:0”

每当我尝试获取随机用户时均会出现java.lang.IndexOutOfBoundsException错误

我需要有关java.lang.IndexOutOfBoundsException-ArrayList的帮助

java.lang.IndexOutOfBoundsException:索引:0,大小:0 Java 8

java.lang.IndexOutOfBoundsException:索引:1,大小:0

java.lang.IndexOutOfBoundsException当RecyclerView显示多个数据

java.lang.IndexOutOfBoundsException:setSpan(N ... N)结束于长度10500

java.lang.IndexOutOfBoundsException:无组1 | Pyspark

java.lang.IndexOutOfBoundsException:源不适合dest

java.lang.IndexOutOfBoundsException:无组1 | 模式匹配

如何在Selenium WebDriver中清除java.lang.IndexOutOfBoundsException

java.lang.IndexOutOfBoundsException:setSpan(4 ... 18)结束于长度6

java.lang.IndexOutOfBoundsException:setSpan(2 ... 2)结束于长度1

材料筹码:检查任何筹码时的 java.lang.IndexOutOfBoundsException

java.lang.IndexOutOfBoundsException:无效的索引50,大小为50

线程“main”中的kotlin异常java.lang.IndexOutOfBoundsException

java.lang.IndexOutOfBoundsException索引:0,大小:0

java.lang.IndexOutOfBoundsException:无效的索引7,大小为7

从array.get获取java.lang.IndexOutOfBoundsException?

ArrayList:java.lang.IndexOutOfBoundsException:索引:283,大小:283

java.lang.IndexOutOfBoundsException:索引:5,大小:5