android intent give NullPointerException in gridview setonitemclickListener event -


android intent give nullpointerexception in gridview setonitemclicklistener event

public class gridfragment extends fragment {      gridview mgridview;     private gridadapter mgridadapter;     griditems[] griditems = {};     private activity activity;      public gridfragment(griditems[] griditems, activity activity) {         this.griditems = griditems;         this.activity = activity;     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {         view view;         view = inflater.inflate(r.layout.grid, container, false);         mgridview = (gridview) view.findviewbyid(r.id.gridview);         return view;     }      @override     public void onactivitycreated(bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);          if (activity != null) {              mgridadapter = new gridadapter(activity, griditems);             if (mgridview != null) {                 mgridview.setadapter(mgridadapter);             }               mgridview.setonitemclicklistener(new onitemclicklistener() {                  @override                  public void onitemclick(adapterview<?> parent, view view, int position, long id) {                       packagemanager pacmanager=getactivity().getpackagemanager();                       intent i=pacmanager.getlaunchintentforpackage(griditems[position].title.tostring());                      getactivity().startactivity(i);                   }              });          }      } 

my error this.

java.lang.nullpointerexception

i getting error on line,

getactivity().startactivity(i); 

what's wrong?

you need replace getactivity().startactivity(i); mainactivity.this.startactivity(i);


quick fix

mgridview.setonitemclicklistener(new onitemclicklistener() {     @override     public void onitemclick(adapterview<?> parent, view view,       int position, long id) {        packagemanager pacmanager=getactivity().getpackagemanager();        intent i=pacmanager.getlaunchintentforpackage(griditems[position].title.tostring());         mainactivity.this.startactivity(i);      }    }); 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -