动态行创建中的Android的IllegalStateException

时间:2013-04-14 10:54:07

标签: java android exception android-tablelayout

我跟踪了这​​个家伙here并最终收到以下错误。据我所知,我不需要在运行时运行新的TableLayout(),也不需要运行新的Textview()

04-14 16:11:55.232: E/AndroidRuntime(24014): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.opencv.samples.fd/org.opencv.samples.fd.StatsActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.os.Looper.loop(Looper.java:137)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.app.ActivityThread.main(ActivityThread.java:4898)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at java.lang.reflect.Method.invokeNative(Native Method)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at java.lang.reflect.Method.invoke(Method.java:511)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at dalvik.system.NativeStart.main(Native Method)
04-14 16:11:55.232: E/AndroidRuntime(24014): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3618)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.view.ViewGroup.addView(ViewGroup.java:3489)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.widget.TableLayout.addView(TableLayout.java:425)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.view.ViewGroup.addView(ViewGroup.java:3434)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.widget.TableLayout.addView(TableLayout.java:407)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.view.ViewGroup.addView(ViewGroup.java:3410)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.widget.TableLayout.addView(TableLayout.java:398)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at org.opencv.samples.fd.StatsActivity.doStats(StatsActivity.java:202)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at org.opencv.samples.fd.StatsActivity.onCreate(StatsActivity.java:64)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.app.Activity.performCreate(Activity.java:5206)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
04-14 16:11:55.232: E/AndroidRuntime(24014):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)

以下是代码段:

public void doStats()
{       
    TableLayout table = (TableLayout) StatsActivity.this.findViewById(R.id.TableLayout01);

    String[] pattern =  splitPattern();         
    for (int i = 0; i < pattern.length; i++)
    {       

        // create a new TableRow
        TableRow row = (TableRow) LayoutInflater.from(StatsActivity.this).inflate(R.layout.attrib_row, null);

        String[] sessions = pattern[i].split("/");

        for (int y = 0; y < sessions.length; y++)
        {
            ((TextView)row.findViewById(R.id.TextView01)).setText(String.valueOf(sessions[0].toString()));
            ((TextView)row.findViewById(R.id.TextView02)).setText(String.valueOf(sessions[1].toString()));
            table.addView(row);             
        }                       
    }
    table.requestLayout(); 
}

请注意,目前我从doStats()开始onCreate。 任何内部都将非常感激。

1 个答案:

答案 0 :(得分:1)

table.addView(row)移出内循环。