StringIndexOutOfBoundsException虽然应用程序仍然运行

时间:2014-09-30 12:28:03

标签: java android

我在Android Studio中遇到此错误:

09-30 14:50:54.929    1698-1698/inco.aplicacion.safrapp.indecipherablecode D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
09-30 14:50:55.080    1698-1698/inco.aplicacion.safrapp.indecipherablecode E/Trace﹕ error opening trace file: No such file or directory (2)
09-30 14:50:56.410    1698-1698/inco.aplicacion.safrapp.indecipherablecode D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
09-30 14:51:03.880    1698-1702/inco.aplicacion.safrapp.indecipherablecode D/dalvikvm﹕ GC_CONCURRENT freed 140K, 3% free 11005K/11271K, paused 23ms+55ms, total 107ms
09-30 15:12:06.719    1783-1783/inco.aplicacion.safrapp.indecipherablecode E/Trace﹕ error opening trace file: No such file or directory (2)
09-30 15:12:11.739    1783-1783/inco.aplicacion.safrapp.indecipherablecode D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
09-30 15:12:19.570    1783-1788/inco.aplicacion.safrapp.indecipherablecode D/dalvikvm﹕ GC_CONCURRENT freed 147K, 3% free 11002K/11271K, paused 30ms+26ms, total 131ms
09-30 15:12:19.590    1783-1783/inco.aplicacion.safrapp.indecipherablecode D/AndroidRuntime﹕ Shutting down VM
09-30 15:12:19.590    1783-1783/inco.aplicacion.safrapp.indecipherablecode W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2f1b288)
09-30 15:12:19.620    1783-1783/inco.aplicacion.safrapp.indecipherablecode E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{inco.aplicacion.safrapp.indecipherablecode/inco.aplicacion.safrapp.indecipherablecode.javacrear}: java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=1
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=1
            at java.lang.String.startEndAndLength(String.java:593)
            at java.lang.String.substring(String.java:1474)
            at inco.aplicacion.safrapp.indecipherablecode.javacrear.CREARCODIGO(javacrear.java:61)
            at inco.aplicacion.safrapp.indecipherablecode.javacrear.onCreate(javacrear.java:18)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
09-30 15:15:37.013    1835-1835/inco.aplicacion.safrapp.indecipherablecode E/Trace﹕ error opening trace file: No such file or directory (2)
09-30 15:15:37.873    1835-1835/inco.aplicacion.safrapp.indecipherablecode D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
09-30 15:16:11.533    1835-1840/inco.aplicacion.safrapp.indecipherablecode D/dalvikvm﹕ GC_CONCURRENT freed 145K, 3% free 11005K/11271K, paused 17ms+45ms, total 91ms
09-30 15:16:11.563    1835-1835/inco.aplicacion.safrapp.indecipherablecode D/AndroidRuntime﹕ Shutting down VM
09-30 15:16:11.563    1835-1835/inco.aplicacion.safrapp.indecipherablecode W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb2f1b288)
09-30 15:16:11.584    1835-1835/inco.aplicacion.safrapp.indecipherablecode E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{inco.aplicacion.safrapp.indecipherablecode/inco.aplicacion.safrapp.indecipherablecode.javacrear}: java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=1
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=1
            at java.lang.String.startEndAndLength(String.java:593)
            at java.lang.String.substring(String.java:1474)
            at inco.aplicacion.safrapp.indecipherablecode.javacrear.CREARCODIGO(javacrear.java:61)
            at inco.aplicacion.safrapp.indecipherablecode.javacrear.onCreate(javacrear.java:18)
            at android.app.Activity.performCreate(Activity.java:5008)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
            at android.app.ActivityThread.access$600(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
09-30 15:17:34.594    1908-1908/inco.aplicacion.safrapp.indecipherablecode E/Trace﹕ error opening trace file: No such file or directory (2)
09-30 15:17:35.447    1908-1908/inco.aplicacion.safrapp.indecipherablecode D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
09-30 15:18:07.294    1956-1956/inco.aplicacion.safrapp.indecipherablecode E/Trace﹕ error opening trace file: No such file or directory (2)
09-30 15:18:08.095    1956-1956/inco.aplicacion.safrapp.indecipherablecode D/gralloc_goldfish﹕ Emulator without GPU emulation detected.

即使我收到该消息,该应用程序仍然在模拟器中运行,并且在我的应用程序的第一个布局中有三个按钮,如果我按下特定按钮,它将崩溃。类的代码(尚未完成):

    package inco.aplicacion.safrapp.indecipherablecode;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

/**
 * Created by SAFR on 9/29/2014.
 */
public class javacrear extends java0 {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.disecrear);
        CREARCODIGO();
    }

    /**
     * CREAR CODIGO:
     */
    private void CREARCODIGO() {
        EditText CODIGO = (EditText) findViewById(R.id.CODIGO);
        EditText CODIGONUMERO = (EditText) findViewById(R.id.CODIGONUMERO);
        TextView CREADO = (TextView) findViewById(R.id.CREADO);
        Button CREAR = (Button) findViewById(R.id.CREAR);
        String A, A1, A2;
        A1 = "";
        String AL[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        A = CODIGO.getText().toString();

        A2 = A;
        int B, X1, X2, X3, X4, V, CONTADO, X, Y;
        X = 0;
        Y = 1;
        B = A.length();
        V = 0;
        /**
         * DIVISION DEL TEXTO:
         */
        if (B <= 10) {
            V = 2;
        } else if (B > 10 && B <= 20) {
            V = 6;
        } else if (B > 20 && B <= 30) {
            V = 10;
        } else if (B > 30 && B <= 40) {
            V = 14;
        } else if (B > 40 && B <= 50) {
            V = 18;
        } else if (B > 50) {
            V = 22;
        }

        A = A.toUpperCase();

        /** ENCRIPTAMIENTO: */
        for (CONTADO = 0; CONTADO == B; CONTADO++) {
            if (A.substring(X, Y).equals(AL[1])) {
                int ZZZ = 1;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[2])) {
                int ZZZ = 2;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[3])) {
                int ZZZ = 3;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[4])) {
                int ZZZ = 4;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[5])) {
                int ZZZ = 5;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[6])) {
                int ZZZ = 6;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[7])) {
                int ZZZ = 7;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[8])) {
                int ZZZ = 8;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[9])) {
                int ZZZ = 9;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[10])) {
                int ZZZ = 10;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[11])) {
                int ZZZ = 11;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[12])) {
                int ZZZ = 12;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[13])) {
                int ZZZ = 13;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[14])) {
                int ZZZ = 14;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[15])) {
                int ZZZ = 15;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[16])) {
                int ZZZ = 16;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[17])) {
                int ZZZ = 17;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[18])) {
                int ZZZ = 18;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[19])) {
                int ZZZ = 19;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[20])) {
                int ZZZ = 20;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[21])) {
                int ZZZ = 21;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[22])) {
                int ZZZ = 22;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[23])) {
                int ZZZ = 23;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[24])) {
                int ZZZ = 24;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[25])) {
                int ZZZ = 25;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            } else if (A.substring(X, Y).equals(AL[26])) {
                int ZZZ = 26;
                ZZZ = ZZZ + V;
                A1 = AL[ZZZ];
                A2 = A2 + A1;
                X = X + 1;
                Y = Y + 1;
            }
        }
        CREAR.setText(A2);
    }
}

1 个答案:

答案 0 :(得分:0)

我看到很多问题:

  1. for (CONTADO = 0; CONTADO == B; CONTADO++)应该有<条件,而不是==
  2. A.substring(X, Y).equals(AL[1])此检查从1开始,一直到26。您将获得ArrayOutOfBoundException
  3. 再次在A.substring(X, Y).equals(AL[1])中,因为您要尝试匹配字符串A的每个字符,那么为什么不使用toCharArray()将其转换为char []。
  4. if区内,您在尝试什么:
  5. int ZZZ = 1;
    ZZZ = ZZZ + V;
    // This will give ArrayOutOfBoundException if value of ZZZ is greater then 25.
    A1 = AL[ZZZ];
    
    1. A2 = A2 + A1;你正在使用大量内存(字符串附加总是一个坏主意)。
    2. 您可以将if块中的代码从单独的函数中拉出来。