android - Dynamically created TextViews in Java - NullPointerException -
i try add dinamically textviews in java. assume when want use settext()
method, should earlier connect java's textview object xml's textview - use setid()
.
at end, got nullpointerexception in line use setid()
.
my code:
textview[] tvquestion = new textview[numberofquestions]; textview[] tvanswer1 = new textview[numberofquestions]; textview[] tvanswer2 = new textview[numberofquestions]; textview[] tvanswer3 = new textview[numberofquestions]; layoutparams params = new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content); (int = 0; < numberofquestions; i++) { tvquestion[i].setid(view.generateviewid()); // nullpointerexception! tvanswer1[i].setid(view.generateviewid()); tvanswer2[i].setid(view.generateviewid()); tvanswer3[i].setid(view.generateviewid()); tvquestion[i].setlayoutparams(params); tvanswer1[i].setlayoutparams(params); tvanswer2[i].setlayoutparams(params); tvanswer3[i].setlayoutparams(params); tvquestion[i].settext(question[i]); tvanswer1[i].settext(option1[i]); tvanswer2[i].settext(option2[i]); tvanswer3[i].settext(option3[i]); layall.addview(tvquestion[i]); layall.addview(tvanswer1[i]); layall.addview(tvanswer2[i]); layall.addview(tvanswer3[i]); }
edit:
solution: philipp jahoda's post.
you created array textviews. the textviews inside array null long not initialized.
so need call
tvquestion[i] = new textview(context); tvanswer[i] = new textview(context); // , on ... // , later tvquestion[i].setid(view.generateviewid()); // , on ...
before setting id , other stuff.
Comments
Post a Comment