这是整个代码运行。它运行了一段时间并且运行良好但随后突然崩溃。我找不到问题。有人可以帮忙吗?这是我制作的第一个应用程序。所有的帮助表示赞赏。提前谢谢。
public void onPostExecute (String result) {
if (result.indexOf("#") == -1){MessageAvCheck = false;}
else if (result.indexOf("#") >= 0)
{
mtake = result.split("#");
final Runnable run = new Runnable() {
public void run() {
StringTokenizer bits = new StringTokenizer(mtake[x], "$");
String mNumber = bits.nextToken();
mphone = bits.nextToken();
mmessage = bits.nextToken();
mphone = mdial.concat(mphone);
SendingMessage.setText(mmessage);
MessageAvCheck = true;
StringTokenizer dt = new StringTokenizer (msDate, " ");
mFile = dt.nextToken();
SendingTime = dt.nextToken();
mFile = mFile.replace("/", "");
SendingTime = SendingTime.replace(":", "");
SendingTime = mFile.concat(SendingTime);
};
if (x == 0){loop.post(run);
}
}
这是应用程序崩溃时logCat提供的内容。
06-01 20:33:20.253: W/dalvikvm(7314): threadid=1: thread exiting with uncaught exception (group=0x40d599a8)
06-01 20:33:20.257: E/AndroidRuntime(7314): FATAL EXCEPTION: main
06-01 20:33:20.257: E/AndroidRuntime(7314): java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
06-01 20:33:20.257: E/AndroidRuntime(7314): at pk.albab.SMSserver.MainActivity$DownloadWebPageTask$1.run(MainActivity.java:101)
06-01 20:33:20.257: E/AndroidRuntime(7314): at android.os.Handler.handleCallback(Handler.java:800)
06-01 20:33:20.257: E/AndroidRuntime(7314): at android.os.Handler.dispatchMessage(Handler.java:100)
06-01 20:33:20.257: E/AndroidRuntime(7314): at android.os.Looper.loop(Looper.java:194)
06-01 20:33:20.257: E/AndroidRuntime(7314): at android.app.ActivityThread.main(ActivityThread.java:5371)
06-01 20:33:20.257: E/AndroidRuntime(7314): at java.lang.reflect.Method.invokeNative(Native Method)
06-01 20:33:20.257: E/AndroidRuntime(7314): at java.lang.reflect.Method.invoke(Method.java:525)
06-01 20:33:20.257: E/AndroidRuntime(7314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
06-01 20:33:20.257: E/AndroidRuntime(7314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-01 20:33:20.257: E/AndroidRuntime(7314): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
当只有1个元素存在时,您正在尝试访问String数组mtake
的第二个元素。
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
当您从mtake
分配result
时,只会从split()
返回一个元素。既然你测试了#'#' char我会说它被找到但是在String的末尾。我会在最后附上一个测试,以说明split()
在不同情况下的工作原理。
您需要先查看result
,然后再继续假设split()
按预期方式成功完成,并解决以下任何一个问题:
split()
for...each
循环ArrayIndexOutOfBoundsException
抛出此处有一项测试,可显示split()
如何在不同位置使用找到的字符:
public static void main(String[] args)
{
String[] strings = {
"no char here",
"in the # middle",
"# was at start",
"now at the end #"
};
for (String str : strings)
{
String[] parts = str.split("#");
System.out.println("'" + str + "', split into: " + parts.length);
for (String s : parts)
{
System.out.println("\t '" + s + "'");
}
}
}
输出:
'no char here', split into: 1
'no char here'
'in the # middle', split into: 2
'in the '
' middle'
'# was at start', split into: 2
''
' was at start'
'at the end #', split into: 1
'now at the end '
正如您所看到的,最后一个案例包含了#'#'但split()
只返回一个长度为1的数组。