i want display array in textview popup window. 1 item array randomly. in case using arraylist in mainactivity , calling randomarray() display. not work on screen_popup.xml. work in main_activity.xml. using textview textview1 main_activity & textview txtviewarraycontent popup window. think textview initialized correctly has setcontentview? pointers great, thank you!
the error in log cat is:
07-17 09:25:34.655 25779-25779/com.example.testarray_02 e/androidruntime﹕ fatal exception: main process: com.example.testarray_02, pid: 25779 java.lang.runtimeexception: unable start activity componentinfo{com.example.testarray_02/com.example.testarray_02.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settext(java.lang.charsequence)' on null object reference main activity java:
public class mainactivity extends activity { button btnclosepopup; button btncreatepopup; textview newarray; string item; //***** random generator & arraylist ***** final random randomgenerator = new random(); final arraylist sample = new arraylist() {{ add("random facts stuff"); add("random facts stuff 2"); add("random facts stuff 3"); add("random facts stuff 4");}}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //works - main activity layout // newarray = (textview)findviewbyid(r.id.textview1); // not work. ... popup layout newarray = (textview)findviewbyid(r.id.txtviewarraycontent); randomarray(); btncreatepopup = (button) findviewbyid(r.id.button1); btncreatepopup.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { custompopupwindow(v); } }); } private void randomarray() { //setcontentview(r.layout.screen_popup); item = (string) sample.get(randomgenerator.nextint(sample.size())); newarray.settext(item); // error logcat points line // null exception error } private popupwindow popupwin; private void custompopupwindow(view v){ try { layoutinflater inflater = (layoutinflater) mainactivity.this .getsystemservice(context.layout_inflater_service); view layout = inflater.inflate(r.layout.screen_popup, (viewgroup) findviewbyid(r.id.popup_element)); popupwin = new popupwindow(layout, 600, 600, true); popupwin.showatlocation(layout, gravity.center, 0, 0); btnclosepopup = (button) layout.findviewbyid(r.id.btn_close_popup); btnclosepopup.setonclicklistener(cancel_button_click_listener); } catch (exception e) { e.printstacktrace(); } } private view.onclicklistener cancel_button_click_listener = new view.onclicklistener() { public void onclick(view v) { popupwin.dismiss(); } }; } snippet of xml screen popupwindow
<textview android:id="@+id/txtviewarraycontent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="5sp" android:text="" />
you trying initialize newarray trying find id in activity_main.xml file, it's not there, nullpointerexception. fix code, remove newarray initialization , call of randomarray() oncreate() method , add in custompopupwindwo(view v) follows:
private void custompopupwindow(view v) { layoutinflater inflater = (layoutinflater) mainactivity.this .getsystemservice(context.layout_inflater_service); view layout = inflater.inflate(r.layout.screen_popup, (viewgroup) findviewbyid(r.id.popup_element)); newarray = (textview) layout.findviewbyid(r.id.txtviewarraycontent); //omitted rest brevity }
Comments
Post a Comment