递归无法按预期工作

时间:2012-06-25 20:22:17

标签: java android recursion

我有一个代码应该得到一个LinearLayout(里面有更多的LinearLayout),并获取其中的所有视图。有些文本视图有标签,据此,它应该构建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)

2 个答案:

答案 0 :(得分:2)

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

因此:

emptyString.length() - 1 = -1

String中的非法索引。

答案 1 :(得分:0)

确保在以下行中:

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

您正在查看term.Length() > 0meaning.Length() > 0

否则你可能会尝试使用参数-1来获取子字符串。