1

我有一个代码应该得到一个线性布局(里面有更多的线性布局),并得到其中的所有视图。有一些带有标签的文本视图,据此,它应该构建 3 个字符串。问题是它在循环后的第一行抛出异常。这是一个代码示例来解释:

private void insertToDB(final DataBaseHelper db, LinearLayout layout, 
                              String subject, String term, String meaning) 
{
    View v1 = null;
    for(int i=0; i < layout.getChildCount(); i++) {
        try {
            v1 = layout.getChildAt(i);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        if(v1 instanceof LinearLayout)
        {
            insertToDB(db, ((LinearLayout)v1), subject, term, meaning);
        }
        if (v1.getTag() != null) {
            String val = v1.getTag().toString();
            if (val.equals("subject")) {
                subject = ((TextView)v1).getText().toString();
            }
            else if (val.equals("term")) {
                term+=((TextView)v1).getText().toString()+"$";
            }
            else if (val.equals("meaning")) {
                meaning+=((TextView)v1).getText().toString()+"$";
            }

        }
    }
    term = term.substring(0, term.length()-1);
    meaning = meaning.substring(0, meaning.length()-1);
    Card c = new Card(subject, term, meaning);
    db.Insert(c);
} 

堆栈跟踪:

06-25 20:27:59.332: D/AndroidRuntime(269): Shutting down VM
06-25 20:27:59.332: W/dalvikvm(269): threadid=1: thread exiting with uncaught exception (group=0x4001d800)

06-25 20:28:01.551: E/AndroidRuntime(269): FATAL EXCEPTION: main
06-25 20:28:01.551: E/AndroidRuntime(269): java.lang.StringIndexOutOfBoundsException
06-25 20:28:01.551: E/AndroidRuntime(269):  at java.lang.String.substring(String.java:1579)
06-25 20:28:01.551: E/AndroidRuntime(269):  at com.ronEven.iCards.AddRemove$1.insertToDB(AddRemove.java:78)
06-25 20:28:01.551: E/AndroidRuntime(269):  at com.ronEven.iCards.AddRemove$1.insertToDB(AddRemove.java:62)
06-25 20:28:01.551: E/AndroidRuntime(269):  at com.ronEven.iCards.AddRemove$1.onClick(AddRemove.java:46)
06-25 20:28:01.551: E/AndroidRuntime(269):  at android.view.View.performClick(View.java:2408)
06-25 20:28:01.551: E/AndroidRuntime(269):  at android.view.View$PerformClick.run(View.java:8816)
06-25 20:28:01.551: E/AndroidRuntime(269):  at android.os.Handler.handleCallback(Handler.java:587)
06-25 20:28:01.551: E/AndroidRuntime(269):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-25 20:28:01.551: E/AndroidRuntime(269):  at android.os.Looper.loop(Looper.java:123)
06-25 20:28:01.551: E/AndroidRuntime(269):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-25 20:28:01.551: E/AndroidRuntime(269):  at java.lang.reflect.Method.invokeNative(Native Method)
06-25 20:28:01.551: E/AndroidRuntime(269):  at java.lang.reflect.Method.invoke(Method.java:521)
06-25 20:28:01.551: E/AndroidRuntime(269):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-25 20:28:01.551: E/AndroidRuntime(269):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-25 20:28:01.551: E/AndroidRuntime(269):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

2

循环之后的其中一个 String.substring 调用显然会导致该问题,因为它们试图从空字符串中提取 String 部分。

所以:

emptyString.length() - 1 = -1

这是字符串中的非法索引。

于 2012-06-25T20:33:52.097 回答
0

确保在这些行中:

term = term.substring(0, term.length()-1);
meaning = meaning.substring(0, meaning.length()-1);

你正在检查那个term.Length() > 0和那个meaning.Length() > 0

否则,您可能会尝试使用参数为 -1 的子字符串。

于 2012-06-25T20:35:08.013 回答