Мені потрібно додати кілька TextViews
до a 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 для досягнення бажаного результату.
Спасибі за вашу допомогу.
StackTrace:
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)
Відповіді:
2 для відповіді № 1Ви створюєте лише одну TextView
змінюючи текст кілька разів, а потім додаючи лише один TextxView
до батьківського макета.
Весь код для його створення повинен бути у вашому циклі
for (Tags tags : tagsArrayList) {
// create it here and add it
}
Я не впевнений, чи вирішить це виняток чи ні, але я думаю, що ви хочете це зробити.
Властивості, які не зміняться, можуть бути оголошені один раз поза циклом, а потім встановлені всередині циклу при його створенні.
Наприклад
int textColor = context.getResources().getColor(R.color.ColorWhite)
потім, всередині вашого циклу
tagsTextView.setTextColor(textColor);