Java string.split()抛出ArrayIndexOutOfBoundsException

时间:2012-10-13 12:29:39

标签: java android arrays string split

我正在尝试在字符串上使用split方法。抛出一个ArrayOutOfBoundsException,我不知道为什么。我尝试了许多字符串和许多分隔符。

    String temp = "abcd&efgh&ijkl&mnop";
    String[] tempArr = temp.split("ijk");

这是错误日志:

10-13 12:20:30.619: E/AndroidRuntime(775): FATAL EXCEPTION: main
10-13 12:20:30.619: E/AndroidRuntime(775): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lino/com.example.lino.LinesActivity}:    java.lang.ArrayIndexOutOfBoundsException
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.os.Looper.loop(Looper.java:123)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-13 12:20:30.619: E/AndroidRuntime(775):  at java.lang.reflect.Method.invokeNative(Native Method)
10-13 12:20:30.619: E/AndroidRuntime(775):  at java.lang.reflect.Method.invoke(Method.java:507)
10-13 12:20:30.619: E/AndroidRuntime(775):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-13 12:20:30.619: E/AndroidRuntime(775):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-13 12:20:30.619: E/AndroidRuntime(775):  at dalvik.system.NativeStart.main(Native Method)
10-13 12:20:30.619: E/AndroidRuntime(775): Caused by: java.lang.ArrayIndexOutOfBoundsException
10-13 12:20:30.619: E/AndroidRuntime(775):  at com.example.lino.LinesActivity.onCreate(LinesActivity.java:32)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-13 12:20:30.619: E/AndroidRuntime(775):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-13 12:20:30.619: E/AndroidRuntime(775):  ... 11 more

修改

这是整个班级:

    package com.example.lino;

    import java.lang.reflect.Array;
    import java.util.ArrayList;
    import java.util.List;

    import android.os.Bundle;
    import android.app.Activity;
    import android.util.TypedValue;
    import android.view.ViewGroup.LayoutParams;
    import android.widget.LinearLayout;
    import android.widget.TextView;


    public class LinesActivity extends Activity {

        TextView tvTitle;
        String bussinessId, bussinessTitle;
        String[] passedString;
        List<Line> linesList;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.lines_layout);

            tvTitle = (TextView) findViewById(R.id.tvMainTitle);

            Bundle passedBundle = getIntent().getExtras();
            passedString = passedBundle.getString("BussinessName").split(":");

            bussinessId = passedString[0];
            bussinessTitle = passedString[1]; // THIS IS LINE 32, BUT IT'S WORKING WELL

            tvTitle.setText(bussinessTitle);

            try {
                String temp = "abcd&efgh&ijkl&mnop";
                String[] tempArr = temp.split("ijk");

            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    }

3 个答案:

答案 0 :(得分:4)

跑步:

String temp = "abcd&efgh&ijkl&mnop";
String[] tempArr = temp.split("ijk");
System.out.println(Arrays.toString(tempArr));

我得到了

[abcd&efgh&, l&mnop]

没有异常。

在我的编辑器中,第32行显示为:

bussinessId = passedString[0];

表示第一个String.split

passedString = passedBundle.getString("BussinessName").split(":");

不是你想要的。

检查passedBundle.getString("BussinessName")的内容,看它是否包含:字符。

答案 1 :(得分:3)

passedString = passedBundle.getString("BussinessName").split(":");

我想你的passedBundle.getString("BussinessName").split(":");

正在返回only one element,您正尝试使用second

访问passedString中的bussinessTitle = passedString[1];元素 在fetching之前

passedString array中的元素打印日志中passedString的长度或

System.out.println(passedString.length);

并且在从passedString数组访问元素之前,最好放置一个条件

if(passedString.length>0)
{
bussinessId = passedString[0];
bussinessTitle = passedString[1]; 

}

答案 2 :(得分:1)

您提供的代码无法抛出异常并崩溃。

我猜你忘了复制如Logcat中所述的LinesActivity的第32行代码。

我很确定你正在读一个不存在的数组:tempArr