Java string.split()正在抛出ArrayIndexOutOfBoundsException
我试图在字符串上使用split方法。这引发了一系列的性感觉,我不知道为什么。我试过很多字符串和分隔符Java string.split()正在抛出ArrayIndexOutOfBoundsException,java,android,arrays,string,split,Java,Android,Arrays,String,Split,我试图在字符串上使用split方法。这引发了一系列的性感觉,我不知道为什么。我试过很多字符串和分隔符 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/AndroidR
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();
}
}
}
运行:
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:
这不是你想要的
检查passedBundle.getStringBusinessName的内容,查看它是否包含:字符。您给出的代码不能引发异常和崩溃 我猜您忘了复制logcat中所述的LinesActivity的代码行32 我很确定你们读到的是数组中不存在的一行:tempArr
passedString = passedBundle.getString("BussinessName").split(":");
我猜你的passedBundle.GetStringBusinessName.split:
仅返回一个元素,您正试图使用businesstitle=passedString[1]从passedString访问第二个元素
在从passedString数组中提取元素之前,请在日志或文件中打印passedString的长度
System.out.println(passedString.length);
在从passedString数组访问元素之前,最好设置一个条件
if(passedString.length>0)
{
bussinessId = passedString[0];
bussinessTitle = passedString[1];
}
该拆分将为您提供长度为2的数组。你是怎么用的?在哪里?您的拆分调用实际上是LinesActivity中的第32行吗?字符串temp=abcd&efgh&ijkl&mnop;字符串[]tempArr=temp.splitijk;forint i=0;i
if(passedString.length>0)
{
bussinessId = passedString[0];
bussinessTitle = passedString[1];
}