我需要将多个 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/