【问题标题】:Activity: You must call removeView() on the child's parent first活动:您必须先在孩子的父母上调用 removeView()
【发布时间】:2017-12-21 03:14:08
【问题描述】:

Android Studio 3.1、Java 1.8。 我想以编程方式将视图添加到 GridLayout。 所以我使用方法 addView()

这是我的活动代码:

public class ProfileActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = getWindow().getDecorView().getRootView();
        View profileCategoryActive = inflater.inflate(R.layout.profile_category_active, (ViewGroup) view, false);
        TextView categoryNameTextView = profileCategoryActive.findViewById(R.id.categoryNameTextView);
        for (int index = 0; index < categoryCount; index++) {
            categoryNameTextView.setText("Hello " + index);
            gridLayout.addView(profileCategoryActive);
        }
    }
}

但我得到运行时错误:

FATAL EXCEPTION: main
Process: com.myproject.android.customer.debug, PID: 4929
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.loyalix.android.customer.debug/com.loyalix.android.customer.ui.ProfileActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:4309)

【问题讨论】:

    标签: android


    【解决方案1】:

    您正在多次添加同一个视图...

    for (...) {
      // can't add _same_ view multiple times!
      gridLayout.addView(profileCategoryActive);
    }
    

    一个视图只能有一个父视图/被添加到另一个视图,因此例外。


    如果你想在一个布局中添加多个视图,那么你需要膨胀多个视图,并分别添加它们。

    for (...) {
      View view = inflate(..) // inflate new view
      layout.addView(view); // add new view
    }
    

    【讨论】:

      【解决方案2】:

      错误信息是不言自明的。

          if(profileCategoryActive.getParent()!=null)   
       ((ViewGroup)profileCategoryActive.getParent()).removeView(profileCategoryActive); // Remove view
       gridLayout.addView(profileCategoryActive);
      

      【讨论】:

        猜你喜欢
        • 2015-06-22
        • 2018-06-05
        • 2013-10-18
        • 2015-09-06
        • 2020-02-16
        • 2014-06-02
        • 1970-01-01
        相关资源
        最近更新 更多