Jake Jake - 24 days ago 9
Android Question

Android's IllegalStateException in dynamic rows creation

I followed this fellow here and ended up getting the following error. As far as I understand I am not required to run new

TableLayout()
, neither new
Textview()
on runtime.

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)


Here's the code snippet:

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();
}


Please note that at the moment I am running
doStats()
from
onCreate
.
Any inside will be much appreciated.

Answer

Move table.addView(row) out of the inner loop.

Comments