【问题标题】:Getting java.lang.RuntimeException while try to add views dynamically to linear layout android尝试将视图动态添加到线性布局android时获取java.lang.RuntimeException
【发布时间】:2015-09-13 14:55:34
【问题描述】:

尝试在 android 中向线性布局添加视图时出现以下崩溃。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.q8car.andriod.activity/com.technivance.q8car.view.FilterItemsActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

我的代码如下

ArrayList<ColorResponse> colors = Q8CarDBManager.getAllColors(FilterItemsActivity.this);
        addColor(-1, null, getString(R.string.no_color));

        if (colors != null && colors.size() > 0) {

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

                addColor(colors.get(i).getId(), colors.get(i).getHexaCode(), colors.get(i).getName());
            }
        }

        updateSelectedColor(-1);

而addColor方法如下

private void addColor(final int colorId, final String colorHexa, final String title) {

        int padding = (int) getResources().getDimension(R.dimen.small_margin);

        if (inflatedView == null) {

            inflatedView = getLayoutInflater().inflate(R.layout.item_color, null);
        }

        if (mLayoutParam == null) {

            mLayoutParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            mLayoutParam.setMargins(0, 0, padding, 0);
        }

        inflatedView.setLayoutParams(mLayoutParam);

        ImageView colorImage = (ImageView)inflatedView.findViewById(R.id.color_image);
        TextView colorTitle = (TextView)inflatedView.findViewById(R.id.color_title);

        int color = -2;
        if (PhoneUtils.isEmpty(colorHexa) == false)
            color = Color.parseColor("#" + colorHexa);

        BitmapDrawable drawable = ImageManager.getInstance().getRoundedShapeWithColor(this, R.drawable.no_color, color , true);
        colorImage.setImageDrawable(drawable);

        colorImage.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                mColorId = colorId;
                updateSelectedColor(colorId);
            }
        });
        colorTitle.setText(title);

//      if(inflatedView.getParent()!=null)
//          ((ViewGroup)inflatedView.getParent()).removeView(inflatedView);

        inflatedView.setId(colorId);
        mColorsImageViews.put(colorId, colorImage);
        mColorsLayout.addView(inflatedView);
    }

请不要这样,当我每次创建膨胀布局时都会花费很多时间。

编辑

这是完整的堆栈跟踪

Version Name2.4.13
Version Code
23Android Version5.0.1
Devicesamsung (GT-I9505)

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.q8car.andriod.activity/com.technivance.q8car.view.FilterItemsActivity}: 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:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
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:4212)
at android.view.ViewGroup.addView(ViewGroup.java:4065)
at android.view.ViewGroup.addView(ViewGroup.java:4010)
at android.view.ViewGroup.addView(ViewGroup.java:3986)
at com.technivance.q8car.view.FilterItemsActivity.addColor(FilterItemsActivity.java:297)
at com.technivance.q8car.view.FilterItemsActivity.initializeUIComponentsData(FilterItemsActivity.java:186)
at com.technivance.q8car.view.FilterItemsActivity.onCreate(FilterItemsActivity.java:98)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
... 10 more

造成崩溃的方法是addColor [mColorsLayout.addView(mColorInflatedView);];

【问题讨论】:

  • 请发布整个堆栈跟踪,并指出该堆栈跟踪中的哪些行对应于上面显示的代码清单中的行。
  • @CommonsWare 请检查我的编辑。
  • "和造成崩溃的方法是 addColor [mColorsLayout.addView(mColorInflatedView);];" -- 你的代码中没有这样的行。你的意思是mColorsLayout.addView(inflatedView);

标签: android android-linearlayout layout-inflater android-inflate


【解决方案1】:

异常消息几乎可以告诉您问题出在哪里。当您已经将其附加到mColorsLayout 时,您的inflatedView 似乎只能不是null,然后您尝试再次附加它。您必须分别为每个视图充气,而不是在类级别为每种颜色声明一个 View 对象。

【讨论】:

  • 这会花费大量时间并减慢应用程序。
  • 那你就得寻找其他的优化手段了。如果你想在屏幕上显示 N 个视图,它们都必须被充气,没有解决方法。
  • 我需要做横向列表的问题,所以我需要自己添加项目。
  • @AmiraElsayedIsmail:这不会奏效。此外,我相当有信心您不能在大多数设备屏幕上同时显示 500 种颜色的视图。使用RecyclerViewAdapterView 的某个变体,而不是您现在正在做的任何事情(大概在LinearLayout 中包含所有500 个ScrollViewHorizontalScrollView)。这样一来,您就不必创建 500 个视图,而只需创建其中的一小部分,在用户滚动时回收它们。
  • @AmiraElsayedIsmail 您只需拨打支持您观点的RecyclerView.Adapter 即可:recyclerView.getAdapter().notifyDataSetChanged()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
  • 1970-01-01
  • 2015-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多