android - 在 LinearLayout 中添加多个 TextView 会导致 stackOverflowError

标签 android view

我需要将多个 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 你必须先在 child 的 parent 身上调用 removeView()。 我需要显示数量不同的 tags,因此我无法使用 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);

关于android - 在 LinearLayout 中添加多个 TextView 会导致 stackOverflowError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31442635/

相关文章:

iOS - 如何在带有半透明黑色状态栏的 Storyboard中回收前 20 像素

wpf - 如何将两个 View 与两个 viemodel 绑定(bind)在一起我 wpf?

android - 如何在 Android 中从 4 边更改 View 的大小?

android - 我在哪里可以找到适用于 Android SDK 的官方 galaxy 选项卡模拟器?

java - 处理 Android Java 应用程序中的 Drive Rest API 错误

android - 应用程序始终在平板电脑中显示横向模式

javascript - View的backbone变化模型

swift - swift 将协议(protocol)委托(delegate)给多个 View

android - 如何使用 OAuth 2.0 验证 flutter 应用程序以访问 Gmail api?

java - 致命自旋挂起/卡在 ThreadID 上