hello trying call function loadfiles()
located in fragmentb
, want call fragmenta
, can refresh gridview
images , videos.
i error same method
fatal exception: main java.lang.nullpointerexception
my method in fragmentb
:
public void loadfiles(){ gridview = (gridview) this.v.findviewbyid(r.id.grid); file f = new file(home); if (f.exists()){ string [] media = f.list(); arraylist<string> files = new arraylist<>(); (int i=0; i<media.length;i++){ if (media[i].endswith(".jpg")|| media[i].endswith(".png")|| media[i].endswith(".jpeg")|| media[i].endswith(".3gp")|| media[i].endswith(".mp4")|| media[i].endswith(".mov")){ files.add(media[i]); } if (i==media.length-1){ gridadapter adapter = new gridadapter(this.v.getcontext(), files, gridview); adapter.notifydatasetchanged(); gridview.setadapter(adapter); } } } }
how call in fragmenta
:
fragmentb b = new fragmentb(); b.loadfiles();
i think problem because of line have tried , there no luck :(
gridadapter adapter = new gridadapter(this.v.getcontext(), files, gridview);
i suggest this:
public class utils{ public static arraylist<string> loadfiles(){ file f = new file(home); if (f.exists()){ string [] media = f.list(); arraylist<string> files = new arraylist<>(); (int i=0; i<media.length;i++){ if (media[i].endswith(".jpg")|| media[i].endswith(".png")|| media[i].endswith(".jpeg")|| media[i].endswith(".3gp")|| media[i].endswith(".mp4")|| media[i].endswith(".mov")){ files.add(media[i]); } } return files; } } }
now in fragmenta , fragmentb etc. can
gridview = (gridview) this.v.findviewbyid(r.id.grid); gridadapter adapter = new gridadapter(this.v.getcontext(), utils.loadfiles(), gridview); gridview.setadapter(adapter);
Comments
Post a Comment