0

我在这里跟着这个家伙,最终得到了以下错误。据我了解,我不需要运行 new TableLayout(),也不需要 newTextview()在运行时运行。

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. 任何内部将不胜感激。

4

1 回答 1

1

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

于 2013-04-14T11:00:29.447 回答