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;
}