java - I am getting Null Pointer Exception in GridView Android -


i getting null pointer exception in fragment using gridview. code is

public class homefragment extends fragment {     gridview grid;     string[] web = { "bata", "service", "puma", "hush" };     int[] imageid = { r.drawable.shoesmall, r.drawable.shoe1, r.drawable.shoe3,             r.drawable.shoe4 };      public homefragment(){}      @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {         view rootview = inflater.inflate(r.layout.fragment_home, container, false);      //   viewgroup root = (viewgroup) inflater.inflate(r.layout.fragment_home, container);          customgrid adapter = new customgrid(getactivity().getapplicationcontext(), web, imageid);         grid = (gridview) getview().findviewbyid(r.id.gridview);         grid.setadapter(adapter);         grid.setonitemclicklistener(new adapterview.onitemclicklistener() {             @override             public void onitemclick(adapterview<?> parent, view view,                     int position, long id) {                 toast.maketext(getactivity().getapplicationcontext(),                         "you clicked @ " + web[+position], toast.length_short)                         .show();             }         });          return rootview;     } } 

can guide why happing? don't use fragments commonly.

your getview() null,because view not yet created,so change

 grid = (gridview) getview().findviewbyid(r.id.gridview); 

to

 grid = (gridview) rootview.findviewbyid(r.id.gridview); 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -