Error when attempting to pass bundle, Android -
i attempting pass selected items on list new activity.
i declared arrayadapter
arrayadapter madapter
before oncreate; in onresume, collect data; , assign madapter
//variable madapter //new arrayadapter string madapter = new arrayadapter<string>( searchingmidwife.this, android.r.layout.simple_list_item_checked, locations); //set list display madapter
and have this, should allow for, when listitem clicked, selected items variable, , pass new activity
protected void onlistitemclick(listview l, view v, int position, long id) { super.onlistitemclick(l, v, position, id); //in checked, in listview item positions checked items sparsebooleanarray checked = l.getcheckeditempositions(); //assign selected items new string arraylist arraylist<string> selecteditems = new arraylist<string>(); //determine size of items checked (number) (int = 0; < checked.size(); i++) { // item position in adapter position = checked.keyat(i); // add location if checked i.e.) == true! if (checked.valueat(i)) //add selected items madapter selecteditems.add(madapter.getitem(position)); } //create string output array, store new string array selected items string[] outputstrarr = new string[selecteditems.size()]; (int = 0; < selecteditems.size(); i++) { outputstrarr[i] = selecteditems.get(i); } intent intent = new intent(getapplicationcontext(), midwiferesultlist.class); // create bundle object bundle b = new bundle(); b.putstringarray("selecteditems", outputstrarr); // add bundle intent. intent.putextras(b); // start resultactivity startactivity(intent); }
i have result activity designed collect selected item , display it:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_midwife_result_list); bundle b = getintent().getextras(); string[] resultarr = b.getstringarray("selecteditems"); listview lv = (listview) findviewbyid(r.id.list); arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, resultarr); lv.setadapter(adapter); }
when run program, can see listitem results, when select 1 program stops, , there error:
unable start activity componentinfo{android.bignerdranch.com.mobilemidwife/android.bignerdranch.com.mobilemidwife.midwiferesultlist}: java.lang.nullpointerexception: storage == null
pointing line of code:
arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, resultarr);
the code seems right...why there null value on resultarr?
b.putstringarray("selecteditems", outputstrarr); ... string[] resultarr = b.getstringarray("selecteditems");
you have typo, in 1 place write "selecteditems" , in write "selecteditems". notice first letter capitalized in 1 not other. should change them same.
Comments
Post a Comment