Tuesday 15 July 2014

java - Android - NullPointerException on a TextView -



java - Android - NullPointerException on a TextView -

i'm having problem can't resolve. i'm declaring , initializing few textviews. when set background in color have nullpointerexception.

here necessary code:

public static arraylist<integer> questlist = new arraylist<integer>(); public static arraylist<textview> scores = new arraylist<textview>(); public static int quest = 50, scoreindex = 0; public boolean first = true, getindex = false; public int corrects = 0, incorrects = 0, index = 0, indexcons = 0; public int correctbutton = 0; public button ans1, ans2, ans3, ans4; linearlayout ln_an1, ln_an2, ln_an3, ln_an4; public textview question; public textview s1, s2, s3, s4, s5, s6, s7, s8, s9, s10; public textview correctstext, correctsnum, incorrectstext, incorrectsnum; public string currentquest; public handler handler = new handler(); @override public void onbackpressed() {} @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); getwindow().requestfeature(window.feature_action_bar); getactionbar().hide(); setcontentview(r.layout.container_layout); if(savedinstancestate != null){} scoreindex = 0; qes.createquestions(); aes.createanswers(); s1 = (textview) findviewbyid(r.id.score1); s2 = (textview) findviewbyid(r.id.score2); s3 = (textview) findviewbyid(r.id.score3); s4 = (textview) findviewbyid(r.id.score4); s5 = (textview) findviewbyid(r.id.score5); s6 = (textview) findviewbyid(r.id.score6); s7 = (textview) findviewbyid(r.id.score7); s8 = (textview) findviewbyid(r.id.score8); s9 = (textview) findviewbyid(r.id.score9); s10 = (textview) findviewbyid(r.id.score10); scores.add(s1);scores.add(s2);scores.add(s3);scores.add(s4); scores.add(s5);scores.add(s6);scores.add(s7);scores.add(s8); scores.add(s9);scores.add(s10); s1.setbackgroundcolor(color.black); s2.setbackgroundcolor(color.black); s3.setbackgroundcolor(color.black); s4.setbackgroundcolor(color.black); s5.setbackgroundcolor(color.black); s6.setbackgroundcolor(color.black); s7.setbackgroundcolor(color.black); s8.setbackgroundcolor(color.black); s9.setbackgroundcolor(color.black); s10.setbackgroundcolor(color.black); if(findviewbyid(r.id.fragment_container) != null){ if(savedinstancestate != null){ return; } tenquestions tq = new tenquestions(); tq.setarguments(getintent().getextras()); getsupportfragmentmanager().begintransaction() .add(r.id.fragment_container, tq).commit(); }`

i have exception @ line "s1.setbackgroundcolor(color.black);"

edit: here container_layout.xml

<framelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fragment_container" tools:context="com.panagetstudio.quiz.tenquestionsgameactivity" > </framelayout>

and here portion of layout textviews :

<linearlayout android:orientation="horizontal" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:padding="2dp" android:background="@color/white"> <textview android:id="@+id/score1" android:layout_width="match_parent" android:textsize="12sp" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center"/> </linearlayout>

(repeated 10 times)

try initializing textviews in fragment calling rootview.findviewbyid(r.id.your_tv_id).

for example, in fragment:

public static class yourfragment extends fragment { @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view rootview = inflater.inflate(r.layout.example_fragment, container, false); textview s1 = (textview) rootview.findviewbyid(r.id.score1); s1.setbackgroundcolor(color.black); } }

it's returning null right because textview objects not in container_layout.xml, activity, not fragment.

java android nullpointerexception textview

No comments:

Post a Comment