子一直给StringIndexOutOfBounds异常 [英] Substring keeps giving StringIndexOutOfBounds exception

查看:140
本文介绍了子一直给StringIndexOutOfBounds异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我用,我已经输入的字符串列表,所以所有的字符串是有的,但是当我尝试使用子对他们来说,这一直给我的StringIndexOutOfBounds例外。

这里的code:

 名单,其中,字符串>李=新的ArrayList<字符串>();
            字符串输入= text.getText()的toString()修剪()。
            的String []查询= entered.split();
            INT温度;
            的for(int i = 0; I< sizeString;我++)
            {
                TEMP = A [1] .indexOf();
                如果(查询[0] .toLowerCase()匹配(A [1] .toLowerCase()子(0,温度)))
                    li.add(A [1]);
            }
 


好了,所以现在我的arrayadapter是给我一个空指针 亚行不会告诉我什么,除了它的空指针 全部code:

 公共无效的onClick(视图v)
        {
            名单<字符串>李=新的ArrayList<字符串>();
            字符串输入= text.getText()的toString()修剪()。
            的String []查询= entered.split();
            INT温度;
            的for(int i = 0; I< sizeString;我++)
            {
                如果(A [1] .indexOf()!=  -  1)
                    {
                        TEMP = A [1] .indexOf();
                        A [1] .substring(0,温度);
                    }
                如果(查询[0] .toLowerCase()。等于(A [1] .toLowerCase()))
                    li.add(A [1]);
            }
            的setContentView(R.layout.search_results);
            最终的ListView列表=(ListView控件)findViewById(R.id.search_list);
            的String []的结果=新的String [sizeString]
            的for(int i = 0; I< li.size();我++)
                结果[I] = li.get(ⅰ);
            list.setAdapter(新ArrayAdapter<字符串>(Browse.this,android.R.layout.simple_list_item_1,结果));
        }
 

解决方案

如果没有空间,那么温度会-1,你应该检查这一点。

So I'm using a list of strings that I have inputted, so all the strings are there, but when I try to use substring on them, it keeps giving me the StringIndexOutOfBounds exception.

Here's the code:

List<String> li = new ArrayList<String>();
            String entered = text.getText().toString().trim();
            String[] query = entered.split(" ");
            int temp;
            for (int i = 0; i < sizeString;i++)
            {
                temp = a[i].indexOf(" ");
                if (query[0].toLowerCase().matches(a[i].toLowerCase().substring(0,temp)))
                    li.add(a[i]);
            }


Okay so now my arrayadapter is giving me a null pointer adb won't tell me what besides its nullpointer Full code:

public void onClick (View v)
        {
            List<String> li = new ArrayList<String>();
            String entered = text.getText().toString().trim();
            String[] query = entered.split(" ");
            int temp;
            for (int i = 0; i < sizeString;i++)
            {
                if ( a[i].indexOf ( " " ) != -1 )
                    {
                        temp = a[i].indexOf(" ");
                        a[i].substring(0,temp);
                    }
                if (query[0].toLowerCase().equals(a[i].toLowerCase()))
                    li.add(a[i]);                   
            }
            setContentView(R.layout.search_results);
            final ListView list= (ListView)findViewById(R.id.search_list);
            String[] results = new String[sizeString];
            for(int i = 0; i<li.size();i++)
                results[i]=li.get(i);
            list.setAdapter(new ArrayAdapter<String>(Browse.this,android.R.layout.simple_list_item_1 ,results));
        }

解决方案

If there is no space, then temp will be -1, you should check for that.

这篇关于子一直给StringIndexOutOfBounds异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆