Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 什么是IndexOutOfBoundsException?我怎样才能修好它?_Java_Android_Android Volley - Fatal编程技术网

Java 什么是IndexOutOfBoundsException?我怎样才能修好它?

Java 什么是IndexOutOfBoundsException?我怎样才能修好它?,java,android,android-volley,Java,Android,Android Volley,这是我的代码: private void bringData() { final TextView mTextView = (TextView) findViewById(R.id.textView); // Instantiate the RequestQueue. RequestQueue queue = Volley.newRequestQueue(this); String url ="http://192.168.4.1:8080/";

这是我的代码:

    private void bringData() {
    final TextView mTextView = (TextView) findViewById(R.id.textView);

    // Instantiate the RequestQueue.
    RequestQueue queue = Volley.newRequestQueue(this);
    String url ="http://192.168.4.1:8080/";

    // Request a string response from the provided URL.
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    // Display the first 500 characters of the response string.
                    mTextView.setText("Response is: "+ response.substring(0,500));
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            mTextView.setText("That didn't work!");
        }
    });
    // Add the request to the RequestQueue.
    queue.add(stringRequest);
}
traceTag是0

我读到一些字符串边界是错误的,但我不知道如何修复它

Error Message:
    java.lang.StringIndexOutOfBoundsException: length=28; regionStart=1;
    regionLength=499 at java.lang.String.substring(String.java:1931) at     
    com.example.my.app.MainActivity$2.onResponse(MainActivity.java:50) at     
    com.example.my.app.MainActivity$2.onResponse(MainActivity.java:46) at     
    com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) at     
    com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) at     
    com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) at android.os.Handler.handleCallback(Handler.java:751) at     
    android.os.Handler.dispatchMessage(Handler.java:95) at     
    android.os.Looper.loop(Looper.java:154) at     
    android.app.ActivityThread.main(ActivityThread.java:6077) at     
    java.lang.reflect.Method.invoke(Native Method) at     
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at     
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
错误描述

There is an IndexOutOfBound exception occurred in your MainActivity class 
Inside second inner class's OnResponse function as shown MainActivity$2onResponse
on line 46 which basically occurred during substring operation in String.java line 1931 
which was invoked from StringRequest.deliverResponse at line 60,
which was invoked from StringRequest.deliverResponse at line 30,
which was invoked from ExecutorDelivery.java at line 99,
which intially started from ZygoteInit$MethodAndArgsCaller's run function 
and reached up-to main thread of ActivityThread.main=>looper=>handler
实际原因

There is an IndexOutOfBound exception occurred in your MainActivity class 
Inside second inner class's OnResponse function as shown MainActivity$2onResponse
on line 46 which basically occurred during substring operation in String.java line 1931 
which was invoked from StringRequest.deliverResponse at line 60,
which was invoked from StringRequest.deliverResponse at line 30,
which was invoked from ExecutorDelivery.java at line 99,
which intially started from ZygoteInit$MethodAndArgsCaller's run function 
and reached up-to main thread of ActivityThread.main=>looper=>handler
您的代码正在尝试使用创建子字符串

starting index = 0
ending index = 500
虽然您的实际响应字符串长度为=28,但字符串长度不足以创建500个字符的子字符串

解决方案:

  • 使用三元运算符验证长度
    ?:

    mTextView.setText("Response is: "+ 
       ((response.length()>499) ? response.substring(0,500) : "length is too short"));
    
    注意:三元运算符(
    ?:
    )是
    if else
    的一个简短表达式,但这意味着它不能作为原子语句出现,因为这是无效的,因为没有赋值

    ((someString.length()>499) ? someString.substring(0,500):"Invalid length");
    
  • if-else
    增强了可视性

    String msg="Invalid Response";
    if(response.length()>499){
        msg=response.substring(0,500);
    }
    mTextView.setText("Response is: "+msg);
    
    //or     mTextView.setText("Response is: "+response);
    
  • 什么是IndexOutOfBoundsException?
    IndexOutOfBoundsException
    RuntimeException
    mean的子类 它是一个未检查的异常,抛出该异常是为了指示索引 某种类型(如数组、字符串或向量)的 范围。例如,使用列表

    如图所示

    IndexOutOfBoundsException的其他变体/子类有哪些?
    • :这表示已使用非法索引访问数组。索引为负数或大于或等于数组的大小,例如

    预防

    String str = "";
    int index =3; 
    if(index < ls.size())    // check, list size must be greater than index
        str = ls.get(index);
    else
        // print invalid index or other stuff
    
    int num = "";
    int index=4;
    if(index < arr.length)     // check, array length must be greater than index
        num = arr[index];
    else
        // print invalid index or other stuff
    
    String name = "FooBar";
    int index = 7;
    char holder = '';
    if(index < name.length())     // check, String length must be greater than index
        holder = name.charAt(index) ;
    else
        // print invalid index or other stuff
    
    预防

    String str = "";
    int index =3; 
    if(index < ls.size())    // check, list size must be greater than index
        str = ls.get(index);
    else
        // print invalid index or other stuff
    
    int num = "";
    int index=4;
    if(index < arr.length)     // check, array length must be greater than index
        num = arr[index];
    else
        // print invalid index or other stuff
    
    String name = "FooBar";
    int index = 7;
    char holder = '';
    if(index < name.length())     // check, String length must be greater than index
        holder = name.charAt(index) ;
    else
        // print invalid index or other stuff
    
    我怎样才能解决这个问题?
  • StackTrace分析
  • 根据空值、长度或有效索引验证输入字符串
  • 使用调试或日志
  • 使用通用异常捕获块
  • 1.)分析堆栈跟踪 如本文开头所示,stacktrace在初始消息中提供了必要的信息,说明了它发生的位置、原因,这样您就可以简单地跟踪代码并应用所需的解决方案

    例如,查找原因
    StringIndexOutOfBoundsException
    ,然后查找指示类文件的
    包名
    ,然后转到该行并记住原因,只需应用解决方案即可

    如果您在文档中研究异常及其原因,这将是一个良好的开端

    2.)根据空值、长度或有效索引验证输入字符串 在不确定的情况下,如果您不知道实际的输入,例如响应来自服务器(或者可能是错误或什么都没有)或用户,那么最好涵盖所有意外情况,尽管相信我,很少有用户总是喜欢推高测试的极限,所以使用
    输入=null&&input.length()>0
    或对于索引,可以使用三元运算符或
    if-else
    边界检查条件

    3.)使用调试或日志 通过在项目中添加断点,您可以在调试模式下测试项目的运行环境,系统将在那里停止,等待您的下一个操作,同时您可以查看变量值和其他详细信息

    日志就像检查点一样,所以当您的控件越过此点时,它们会生成详细信息,基本上它们是威瑟尔系统提供的信息性消息,或者用户也可以使用日志或Println消息放置日志消息

    4.)使用通用异常捕获块
    Try-catch
    块在处理
    RuntimeExceptions
    时总是很有用的,因此您可以使用多个
    catch
    块来处理可能出现的问题并提供适当的详细信息

    try {
         mTextView.setText("Response is: "+ response.substring(0,500));
    } catch (IndexOutOfBoundsException e) {
        e.printStackTrace();
        System.out,println("Invalid indexes or empty string");
    }
      catch (NullPointerException e) { // mTextView or response can be null 
        e.printStackTrace();
        System.out,println("Something went wrong ,missed initialization");
    }
    catch (Exception e) {  
        e.printStackTrace();
        System.out,println("Something unexpected happened , move on or can see stacktrace ");
    }
    
    进一步参考

    错误描述

    There is an IndexOutOfBound exception occurred in your MainActivity class 
    Inside second inner class's OnResponse function as shown MainActivity$2onResponse
    on line 46 which basically occurred during substring operation in String.java line 1931 
    which was invoked from StringRequest.deliverResponse at line 60,
    which was invoked from StringRequest.deliverResponse at line 30,
    which was invoked from ExecutorDelivery.java at line 99,
    which intially started from ZygoteInit$MethodAndArgsCaller's run function 
    and reached up-to main thread of ActivityThread.main=>looper=>handler
    
    实际原因

    There is an IndexOutOfBound exception occurred in your MainActivity class 
    Inside second inner class's OnResponse function as shown MainActivity$2onResponse
    on line 46 which basically occurred during substring operation in String.java line 1931 
    which was invoked from StringRequest.deliverResponse at line 60,
    which was invoked from StringRequest.deliverResponse at line 30,
    which was invoked from ExecutorDelivery.java at line 99,
    which intially started from ZygoteInit$MethodAndArgsCaller's run function 
    and reached up-to main thread of ActivityThread.main=>looper=>handler
    
    您的代码正在尝试使用创建子字符串

    starting index = 0
    ending index = 500
    
    虽然您的实际响应字符串长度为=28,但字符串长度不足以创建500个字符的子字符串

    解决方案:

  • 使用三元运算符验证长度
    ?:

    mTextView.setText("Response is: "+ 
       ((response.length()>499) ? response.substring(0,500) : "length is too short"));
    
    注意:三元运算符(
    ?:
    )是
    if else
    的一个简短表达式,但这意味着它不能作为原子语句出现,因为这是无效的,因为没有赋值

    ((someString.length()>499) ? someString.substring(0,500):"Invalid length");
    
  • if-else
    增强了可视性

    String msg="Invalid Response";
    if(response.length()>499){
        msg=response.substring(0,500);
    }
    mTextView.setText("Response is: "+msg);
    
    //or     mTextView.setText("Response is: "+response);
    
  • 什么是IndexOutOfBoundsException?
    IndexOutOfBoundsException
    RuntimeException
    mean的子类 它是一个未检查的异常,抛出该异常是为了指示索引 某种类型(如数组、字符串或向量)的 范围。例如,使用列表

    如图所示

    IndexOutOfBoundsException的其他变体/子类有哪些?
    • :这表示已使用非法索引访问数组。索引为负数或大于或等于数组的大小,例如

    预防

    String str = "";
    int index =3; 
    if(index < ls.size())    // check, list size must be greater than index
        str = ls.get(index);
    else
        // print invalid index or other stuff
    
    int num = "";
    int index=4;
    if(index < arr.length)     // check, array length must be greater than index
        num = arr[index];
    else
        // print invalid index or other stuff
    
    String name = "FooBar";
    int index = 7;
    char holder = '';
    if(index < name.length())     // check, String length must be greater than index
        holder = name.charAt(index) ;
    else
        // print invalid index or other stuff
    
    预防

    String str = "";
    int index =3; 
    if(index < ls.size())    // check, list size must be greater than index
        str = ls.get(index);
    else
        // print invalid index or other stuff
    
    int num = "";
    int index=4;
    if(index < arr.length)     // check, array length must be greater than index
        num = arr[index];
    else
        // print invalid index or other stuff
    
    String name = "FooBar";
    int index = 7;
    char holder = '';
    if(index < name.length())     // check, String length must be greater than index
        holder = name.charAt(index) ;
    else
        // print invalid index or other stuff
    
    我怎样才能解决这个问题?
  • StackTrace分析
  • 根据空值、长度或有效索引验证输入字符串
  • 使用调试或日志
  • 使用通用异常捕获块
  • 1.)分析堆栈跟踪 如本文开头所示,stacktrace在初始消息中提供了必要的信息,说明了它发生的位置、原因,这样您就可以简单地跟踪代码并应用所需的解决方案

    例如,查找原因
    StringIndexOutOfBoundsException
    ,然后查找指示类文件的
    包名
    ,然后转到该行并记住原因,只需应用解决方案即可

    如果您在文档中研究异常及其原因,这将是一个良好的开端

    2.)根据空值、长度或有效索引验证输入字符串 在不确定的情况下,当您不知道实际输入时,例如响应来自服务器(或者可能是错误或什么都没有)或用户t