Attempt to invoke virtual method 'boolean android.widget.CheckBox.isChecked()' on a null object reference -


i trying display selected items values because want store them later arraylist send them server. when select items , click on button app crashes , gettting fowllowing error: attempt invoke virtual method 'boolean android.widget.checkbox.ischecked()' on null object reference

how can fix that?

i appreciate help.

oncreate method in mainactivity:

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.route_available);      arraylist<integer> arr = new arraylist<integer>();     arr.add(2);     arr.add(4);     arr.add(6);     createcheckboxlist(arr);  } 

the createcheckboxlist method in mainactivity:

 private void createcheckboxlist(final arraylist<integer> items) {           final linearlayout ll = (linearlayout) findviewbyid(r.id.lila);         (int = 0; < items.size(); i++) {            checkbox cb = new checkbox(this);             cb.settext(string.valueof(items.get(i)));             cb.setid(i);             ll.addview(cb);          }         button btn = new button(this);         btn.setlayoutparams(new linearlayout.layoutparams(500, 150));         btn.settext("submit");         ll.addview(btn);          btn.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                 (int : items) {                     //boolean checked = ((checkbox)v).ischecked();                     checkbox ch=(checkbox) findviewbyid(i);                      if (ch.ischecked()) {                          toast.maketext(getapplicationcontext(),                                     "button clicked" + ch, toast.length_long).show();                      }                 }              }         });      } 

xml file:

<linearlayout 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:orientation="vertical"     android:paddingbottom="@dimen/activity_vertical_margin"     android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     tools:context="com.bustracker.mainactivity" >      <textview         android:id="@+id/textview1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_margintop="5dp"         android:paddingleft="10dp"         android:text="select route: "         android:textsize="30sp" />      <linearlayout         android:id="@+id/lila"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_weight="0.75"         android:orientation="vertical" >     </linearlayout>    </linearlayout> 

change first loop this

for (int = 0; < items.size(); i++) {        checkbox cb = new checkbox(this);         cb.settext(string.valueof(items.get(i)));         cb.setid(items.get(i));         ll.addview(cb);      } 

edit: display values in single toast, can like

@override         public void onclick(view v) {             string message = "";             (int : items) {                 //boolean checked = ((checkbox)v).ischecked();                 checkbox ch=(checkbox) findviewbyid(i);                  if (ch.ischecked()) {                     message = message + " " + ch.gettext() + " " ;                                    }             }             toast.maketext(getapplicationcontext(),                                 "button clicked " + message, toast.length_long).show();          } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -