我需要将多个TextViews添加到LinearLayout

但是代码正在创建某种导致stackOverflowError的周期性依赖关系。

 LinearLayout tagsLayout;
 tagsLayout = (LinearLayout) rootView.findViewById(R.id.pager_item_tags);

  if (!tagsArrayList.isEmpty()) {
      TextView tagTitle = new TextView(context);
      tagTitle.setText("Tags: ");
      tagsLayout.addView(tagTitle, 0);
      TextView tagsTextView = new TextView(context);
      tagsTextView.setGravity(TextView.TEXT_ALIGNMENT_CENTER);
      tagsTextView.setBackgroundResource(R.drawable.bg_tag);
      tagsTextView.setTextColor(context.getResources().getColor(R.color.ColorWhite));
      tagsTextView.setPadding(4, 0, 4, 0);

      for (Tags tags : tagsArrayList) {
          tagsTextView.setText(tags.getName());

         /*generating some kind of cyclic dependency*/

        if (tagsTextView.getParent() != null) {
            ((ViewGroup) tagsTextView.getParent()).removeView(tagsTextView);
        }

        tagsLayout.addView(tagsTextView);
     }

  }


如果我不使用getParent().removeView()
我得到java.lang.IllegalStateException you must call removeView() on the child's parent first.
我需要显示数量不同的标签,因此,我无法使用xml来获得所需的结果。

谢谢你的帮助。

堆栈跟踪:

  java.lang.StackOverflowError: stack size 8MB
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6081)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)
            at android.view.ViewGroup.resetResolvedLayoutDirection(ViewGroup.java:6082)


        android.os.TransactionTooLargeException
            at android.os.BinderProxy.transactNative(Native Method)
            at android.os.BinderProxy.transact(Binder.java:496)
            at android.app.ActivityManagerProxy.handleApplicationCrash(ActivityManagerNative.java:4105)
            at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:89)
            at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
            at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)

最佳答案

您仅创建一个TextView多次更改文本,然后仅将一个TextxView添加到父版式。

所有创建它的代码都应该在循环中

 for (Tags tags : tagsArrayList) {
    // create it here and add it
}


我不确定这是否可以解决异常,但这是我想您要执行的操作。

不变的属性可以在循环外声明一次,然后在创建循环时在循环内设置。

例如

int textColor = context.getResources().getColor(R.color.ColorWhite)


然后,在你的循环内

tagsTextView.setTextColor(textColor);

08-17 22:05