在 LinearLayout 中添加多个 TextView 会导致 stackOverflowError
Adding multiple TextView(s) in a LinearLayout causes stackOverflowError
我需要将多个 TextViews
添加到一个 LinearLayout
。
但是代码正在创建某种循环依赖性,这导致 WhosebugError
。
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.
我需要显示数量不同的 tags,所以我无法使用 xml 来达到预期的结果。
感谢您的帮助。
堆栈跟踪:
java.lang.WhosebugError: 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);
我需要将多个 TextViews
添加到一个 LinearLayout
。
但是代码正在创建某种循环依赖性,这导致 WhosebugError
。
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.
我需要显示数量不同的 tags,所以我无法使用 xml 来达到预期的结果。
感谢您的帮助。
堆栈跟踪:
java.lang.WhosebugError: 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);