【问题标题】:android studio java.lang.ArrayIndexOutOfBoundsException: length=1; index=1android studio java.lang.ArrayIndexOutOfBoundsException: 长度=1;索引=1
【发布时间】:2023-04-10 23:18:01
【问题描述】:

这是我的代码;

字符串 KLMN = isekle_dosyaucreti.getText().toString(); if (!KLMN.contains(",")) {

        KLMN = KLMN + ",00";
        Toast.makeText(getApplicationContext(), KLMN, Toast.LENGTH_SHORT).show();

    }
    else if(KLMN.contains(",")){

        String[] data = KLMN.split(",", 2); //before comma
        String[] xab = KLMN.split(",");
        String nn = xab[0];
        String mm = xab[1]; // after comma

        if(mm.length() < 2) {
            KLMN = data[0].concat("," + mm).concat("0");
            Toast.makeText(getApplicationContext(), KLMN, Toast.LENGTH_SHORT).show();
        }

        else if(mm.length() == 2) {
            KLMN = data[0].concat(","+xab[1]);
            Toast.makeText(getApplicationContext(), KLMN, Toast.LENGTH_SHORT).show();
        }

        else if(mm.length() > 2) {
            Toast.makeText(getApplicationContext(), "The number after the strike cannot be larger than 2 digits", Toast.LENGTH_SHORT).show();
        }

        else if(mm.length() == 0) {
            Toast.makeText(getApplicationContext(), "Should be after the vigrul", Toast.LENGTH_SHORT).show();
        }

    }

--------- This is the error: beginning of crash

2020-04-11 23:04:33.560 609-609/com.nicatalibli.bilirkisiasistanti E/AndroidRuntime: FATAL EXCEPTION: main 进程:com.nicatalibli.bilirkisiasistanti,PID:609 java.lang.ArrayIndexOutOfBoundsException:长度=1;索引=1 在 com.nicatalibli.bilirkisiasistanti.Activity.BottomActivity.isekle.Deneme(isekle.java:1422) 在 com.nicatalibli.bilirkisiasistanti.Activity.BottomActivity.isekle.onClick(isekle.java:1462) 在 android.view.View.performClick(View.java:6597) 在 android.view.View.performClickInternal(View.java:6574) 在 android.view.View.access$3100(View.java:778) 在 android.view.View$PerformClick.run(View.java:25885) 在 android.os.Handler.handleCallback(Handler.java:873) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:6669) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

错误行;

字符串 mm = xab[1]; >

【问题讨论】:

    标签: android


    【解决方案1】:

    Length=1 表示数组只有一个元素并且位于第 0 位,因为数组从第 0 位开始,您正在访问数组中的第 1(index=1) 个位置元素或进行一些操作在第 1 位。这就是错误所表明的。在 KLMN 中,“,”之后没有任何数字,这就是为什么没有您尝试访问的 xab[1] 元素。 现在调试您的代码并检查数组(选项卡)的实际长度。

    【讨论】:

    • 感谢您的回答。我只想用 Toast 显示错误“在 KLMN 中,“,”之后没有任何数字。我该怎么做?
    • 我想要这个:用户在edittex中写了样本“125”(没有在逗号后写数字)并按下“确定按钮”。我希望出现错误消息。
    • 这又是一个不同的问题,但答案在这里,来自 EditText 的按钮 getText() 的 onClickListner() 并检查逗号后是否没有数字然后显示 toast。 Toast.makeText(getApplicationContext(),"你的消息说逗号后面没有数字",Toast.LENGTH_SHORT).show();
    • 我无法完全理解你在说什么。我刚开始。你能给我一个带代码的例子吗?
    • 您需要阅读有关 TextWatcher() 的信息。您必须实现 et.addTextChangedListener() ,您将在其中获得 afterTextChanged() 方法来覆盖。在此方法中,您将看到 EditText 中的每个输入,您可以编写逻辑来检查“,”之后是否有任何数字。我希望这会有所帮助并给你一些想法。检查下面的TextWatcher帖子stackoverflow.com/questions/8543449/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2018-11-05
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    相关资源
    最近更新 更多