GridView: Attempt to invoke virtual method.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup)' on a null object reference -
i have listview
, i'm trying go gridview
onitemcliclick listener. have exception on adapter put can't solve it.
the list view fragment , works well!
the exception is:
java.lang.nullpointerexception: attempt invoke virtual method 'android.view.view android.view.layoutinflater.inflate(int, android.view.viewgroup)' on null object reference @ com.rt_no.myprogect.ui.adapter.careerimageadapter.getview(careerimageadapter.java:52)
at line in gridview
adapter:
convertview = layoutinflater.inflate(r.layout.career_grid_view, null);
the listview:
public class citiesfragment extends fragment { private static final string tag ="citiesfragment"; private listview citylist; private arraylist<city> cities; private citiesadapter citiesadapter; private citiesfragmentlistener listener; private context context; private gridview gridview; @override public void onattach(context context) { super.onattach(context); log.d(tag, "onattach_citiesfragment"); listener = (citiesfragmentlistener) context; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { log.d(tag, "oncreateview_citiesfragment"); view view = inflater.inflate(r.layout.cities_list_view_fragment, null); return view; } private arraylist<city> initcities(){.....} private void initgridview(fragment fragment){ log.d(tag, "citiesfragment_initgridview"); fragmenttransaction fragmenttransaction = getfragmentmanager().begintransaction(); fragmenttransaction.replace(r.id.container,fragment); fragmenttransaction.addtobackstack("fragment"); fragmenttransaction.settransition(fragmenttransaction.transit_fragment_fade); fragmenttransaction.commit(); } @override public void onviewcreated(view view, @nullable bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); log.d(tag, "onviewcreated_citiesfragment"); cities = initcities(); citylist = (listview) view.findviewbyid(r.id.citylist); citiesadapter = new citiesadapter(getactivity(), cities); citylist.setadapter(citiesadapter); citylist.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { log.d(tag, "onitemclick_citiesfragment position : " + cities.get(position).getcityname()); gridviewfragment fragment = new gridviewfragment(); initgridview(fragment); } }); }
the gridview:
public class gridviewfragment extends fragment { private static final string tag = "gridviewfragment"; private citiesfragmentlistener listener; private gridview gridview; private careerimageadapter careerimageadapter; private int position; @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, bundle savedinstancestate) { log.d(tag, "oncreateview_gridviewfragment"); view view = inflater.inflate(r.layout.career_grid_view, null); gridview = (gridview) view.findviewbyid(r.id.careergridview); return view; } @override public void onviewcreated(view view, @nullable bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); log.d(tag, "onviewcreated_gridviewfragment"); gridview = (gridview) view.findviewbyid(r.id.careergridview); gridview.setadapter(new careerimageadapter(getactivity())); gridview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { log.d(tag, "onitemclick_gridviewfragment position : " + position); // go workers_list_view_fragment layout } }); }
the gridview adapter:
public class careerimageadapter extends baseadapter { private static final string tag = "careerimageadapter"; private context mcontext; private layoutinflater layoutinflater; public careerimageadapter (context c){ mcontext = c; } @override public int getcount() { return imageides.length; } @override public careerimageadapter getitem(int position) { return null; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { imageview imageview ; if (convertview == null){ convertview= layoutinflater.inflate(r.layout.career_grid_view, null); imageview = new imageview(mcontext); imageview.setlayoutparams(new gridview.layoutparams(180, 180)); imageview.setscaletype(imageview.scaletype.fit_end); imageview.setpadding(8, 8, 8, 8); }else { imageview = (imageview) convertview; } imageview.setimageresource(imageides[position]); return convertview; } private integer[] imageides = { r.drawable.afar,r.drawable.asbaka,r.drawable.bnya, r.drawable.cnstractors,r.drawable.doors,r.drawable.etom, r.drawable.floring,r.drawable.foxy,r.drawable.garnolet, r.drawable.gaz,r.drawable.geves,r.drawable.ginoon, r.drawable.halbashateven,r.drawable.hashmal,r.drawable.instltsya, r.drawable.kvesatshatyah,r.drawable.mazgan,r.drawable.meshlowah, r.drawable.pargolot,r.drawable.pegom,r.drawable.pinoyafpa, r.drawable.robah,r.drawable.tyah, }; }
layoutinflater
never initialized. add in careerimageadapter
constructor:
layoutinflater = layoutinflater.from(c);
Comments
Post a Comment