-4

Why below codes lead to different, code-1 crashes, code-2 is fine. This code is to output the value of ArrayList(which is one, two, three, etc) as individual TextView to a LinearLayout.

CODE-1

LinearLayout rootView = (LinearLayout) findViewById(R.id.rootView);
TextView numberView = new TextView(this);
    int index = 0;
    while (index < 10){
        numberView.setText(words.get(index));
        rootView.addView(numberView);
        index = index + 1;
    }

CODE-2

LinearLayout rootView = (LinearLayout) findViewById(R.id.rootView);
    int index = 0;
    while (index < 10){
        TextView numberView = new TextView(this);
        numberView.setText(words.get(index));
        rootView.addView(numberView);
        index = index + 1;
    }
Lyman
  • 1
  • 1
  • Possible duplicate : http://stackoverflow.com/questions/3728062/what-is-the-meaning-of-this-in-java – Mehraj Malik May 15 '17 at 06:15
  • 2
    A view can have only 1 parent view. In the code 2 you create a new and fresh `TextView` that does not have a parent yet. In code 1 you use the same `TextView` over and over again. The first time the loops execute everything goes well, because the view does not have a parent yet. The second time the loop already has a parent and your crashes. This is also what your stacktrace should tell you by the way – 0xDEADC0DE May 15 '17 at 06:17
  • write the exception you are getting – Arjit May 15 '17 at 06:21

2 Answers2

0

The difference between those 2 is

In part 1, you are adding view(TextView) to the LinearLayout inside a loop. In every iteration you are adding same instance every time. While in part 2 you are always creating a new instance of TextView in every iteration and adding to the LinearLayout. And What I believe, you are adding same instance multiple times to the same parent layout that is causing problem but am not sure. Hope that helps

Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
0

Hopefully you already know that generally, code in a loop will be executed multiple times, and code outside a loop will only be executed once.

In CODE-1, this line is executed only once:

TextView numberView = new TextView(this);

This means that there is only one TextView that is ever created.

In the loop, you have these lines:

numberView.setText(words.get(index));
rootView.addView(numberView);
index = index + 1;

You are setting the text of the TextView again and again. And you are adding the TextView as a subview to rootView again and again.

First of all, setting the text of the same text view multiple times in a loop will just cause the text view to show the last text you set, ignoring all the intermediate ones.

Second of all, adding a view as a subview to another view multiple times makes no sense at all. This is also the cause of the exception, probably. A view can only have one parent, so it can't be added as a subview a second time, without removing it from the parent beforehand.

In CODE-2, you put the initialisation of the text view inside the loop. This will create multiple text views and you add each text view as a subview to rootView, so no problems here.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • @Lyman If you think my answer answers your question, please consider accepting it by clicking on that checkmark! – Sweeper May 16 '17 at 05:58
  • Just want to confirm that add a view to another for multiple times will cause crash or overwriting? You said "A view can only have one parent", but I am adding it to the same parent, is removing necessary before adding again? Thanks – Lyman May 16 '17 at 05:59
  • @Lyman Sorry if I did not explained very well. When you call `addView`, it does not check whether the view is already in the parent's subviews. Yes you can remove it first, then add it back in, but then you will be adding and removing one single view throughout the loop. I think what you really want is to add multiple text views to the linear layout, right? – Sweeper May 16 '17 at 06:06
  • I understand your explanation. The only left question is this, you said it won't check if the view has been assigned before trying to assign it again, then just for CODE-1, why can't the loop just set value of numberView and add it to rootView for 10 times, why the app has to crash? 10 times is not a endless loop. Thanks again. – Lyman May 17 '17 at 03:48
  • @Lyman what I mean is that the view does not check whether the parent it now has is the same as the one you are adding it to. The parent does check whether its sub views already have a parent, – Sweeper May 17 '17 at 04:32