Android:linearLayout。新行和指定子项已经有父项

时间:2014-09-11 07:01:58

标签: android eclipse android-layout android-linearlayout parent-child

我找到了新线问题的解决方案,但我似乎无法让它工作,我一直在检查每一条评论,我似乎找不到任何与我有同样问题的人。 Android - multi-line linear layout 我一直试图在StackOverFlow上找到类似的线程,但是找不到任何线程。

我的代码:

public void setUpListToLinearLayout(LinearLayout l1, ArrayList collection, String header){

    Display display = getWindowManager().getDefaultDisplay();
    int maxWidth = display.getWidth() - 10;

    if (collection.size()>0){
        LinearLayout l1Also = new LinearLayout(this);
        l1Also.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        l1Also.setOrientation(LinearLayout.HORIZONTAL);

        TextView txtSample = new TextView(this);
        txtSample.setText(header);

        l1Also.addView(txtSample);
        txtSample.measure(0, 0);

        int widthSoFar = txtSample.getMeasuredWidth();

        for (int i = 0; i < collection.size(); i ++){

            TextView txtSamItem = new TextView(this);
            txtSamItem.setText(collection.get(i).toString());
            txtSamItem.setPadding(10, 0, 10, 0);
            txtSamItem.setTag(collection.get(i));
            txtSamItem.measure(0, 0);
            widthSoFar += txtSamItem.getMeasuredWidth();

            if (widthSoFar>=maxWidth){
                l1.addView(l1Also);
                l1Also.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                l1Also.setOrientation(LinearLayout.HORIZONTAL);

                l1Also.addView(txtSamItem);
                widthSoFar = txtSamItem.getMeasuredWidth();


            } else {
                l1Also.addView(txtSamItem);
            }

        }
        l1.addView(l1Also);

    }
}

我在logcat中得到这个:

E/AndroidRuntime(10872): FATAL EXCEPTION: main
E/AndroidRuntime(10872): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xiloandroid/activities.DetailedOpenCasesActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
E/AndroidRuntime(10872):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2265)
E/AndroidRuntime(10872):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2315)
E/AndroidRuntime(10872):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
E/AndroidRuntime(10872):    at     android.app.ActivityThread$H.handleMessage(ActivityThread.java:1297)
E/AndroidRuntime(10872):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(10872):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(10872):    at android.app.ActivityThread.main(ActivityThread.java:5235)
E/AndroidRuntime(10872):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(10872):    at java.lang.reflect.Method.invoke(Method.java:525)             E/AndroidRuntime(10872):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:742)
E/AndroidRuntime(10872):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
E/AndroidRuntime(10872):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(10872): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
E/AndroidRuntime(10872):    at android.view.ViewGroup.addViewInner(ViewGroup.java:3509)
E/AndroidRuntime(10872):    at android.view.ViewGroup.addView(ViewGroup.java:3380)
E/AndroidRuntime(10872):    at android.view.ViewGroup.addView(ViewGroup.java:3325)
E/AndroidRuntime(10872):    at android.view.ViewGroup.addView(ViewGroup.java:3301)
E/AndroidRuntime(10872):    at activities.DetailedOpenCasesActivity.setUpListToLinearLayout(DetailedOpenCasesActivity.java:287)
E/AndroidRuntime(10872):    at activities.DetailedOpenCasesActivity.update(DetailedOpenCasesActivity.java:242)
E/AndroidRuntime(10872):    at activities.DetailedOpenCasesActivity.onCreate(DetailedOpenCasesActivity.java:98)
E/AndroidRuntime(10872):    at android.app.Activity.performCreate(Activity.java:5133)
E/AndroidRuntime(10872):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(10872):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2229)

我认为可能是因为这一行:

l1.addView(l1Also);

我已经被困在这个问题中半天了,我真的很感激一些帮助。

2 个答案:

答案 0 :(得分:0)

您已拨打l1.addView(l1Also);两次。 in if()一次,结束一次。 所以删除它并再次添加。

所以最后调用它。

l1.removeView(l1Also);

然后

l1.addView(l1Also);

答案 1 :(得分:0)

您不应该在循环中一次又一次地向父布局添加相同的视图。导致错误:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

而不是这样,你应该在循环中采用一个新的线性布局l2Also,并将其添加到主布局中。:

请参阅此代码段:

for (int i = 0; i < collection.size(); i ++){

    LinearLayout l2Also = new LinearLayout(this);
    l2Also.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    l2Also.setOrientation(LinearLayout.HORIZONTAL);
    TextView txtSamItem = new TextView(this);
    txtSamItem.setText(collection.get(i).toString());
    txtSamItem.setPadding(10, 0, 10, 0);
    txtSamItem.setTag(collection.get(i));
    txtSamItem.measure(0, 0);
    widthSoFar += txtSamItem.getMeasuredWidth();

    if (widthSoFar>=maxWidth){

        l2Also.addView(txtSamItem);  //in a new linear layout, your text will be saved.
        widthSoFar = txtSamItem.getMeasuredWidth();
    } else {
        l2Also.addView(txtSamItem);
    }
    l1Also.addView(l2Also);  // that new layout will be added in l1Also at each iteration
}
l1.addView(l1Also);  // finally add l1Also in parent layout.

P.S:您需要将布局l1Also的方向更改为VERTICAL,以便在新行中添加新布局l2Also

希望这有帮助。