android - Unable to start activity ComponentInfo java.lang.NullPointerException File Browser -
i have copied class, opens dialog in order pick file, tutorial , don't know why throws me exception, can me?
this class :
import android.app.activity; import android.app.alertdialog; import android.app.dialog; import android.content.dialoginterface; import android.os.environment; import android.util.log; import java.io.file; import java.io.filenamefilter; import java.util.arraylist; import java.util.list; public class filedialog { private static final string parent_dir = ".."; private final string tag = getclass().getname(); private string[] filelist; private file currentpath; public interface fileselectedlistener { void fileselected(file file); } public interface directoryselectedlistener { void directoryselected(file directory); } private listenerlist<fileselectedlistener> filelistenerlist = new listenerlist<filedialog.fileselectedlistener>(); private listenerlist<directoryselectedlistener> dirlistenerlist = new listenerlist<filedialog.directoryselectedlistener>(); private final activity activity; private boolean selectdirectoryoption; private string fileendswith; public filedialog(activity activity, file path) { this.activity = activity; if (!path.exists()) path = environment.getexternalstoragedirectory(); loadfilelist(path); } public dialog createfiledialog() { dialog dialog = null; alertdialog.builder builder = new alertdialog.builder(activity); builder.settitle(currentpath.getpath()); if (selectdirectoryoption) { builder.setpositivebutton("select directory", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { log.d(tag, currentpath.getpath()); firedirectoryselectedevent(currentpath); } }); } builder.setitems(filelist, new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int which) { string filechosen = filelist[which]; file chosenfile = getchosenfile(filechosen); if (chosenfile.isdirectory()) { loadfilelist(chosenfile); dialog.cancel(); dialog.dismiss(); showdialog(); } else firefileselectedevent(chosenfile); } }); dialog = builder.show(); return dialog; } public void addfilelistener(fileselectedlistener listener) { filelistenerlist.add(listener); } public void removefilelistener(fileselectedlistener listener) { filelistenerlist.remove(listener); } public void setselectdirectoryoption(boolean selectdirectoryoption) { this.selectdirectoryoption = selectdirectoryoption; } public void adddirectorylistener(directoryselectedlistener listener) { dirlistenerlist.add(listener); } public void removedirectorylistener(directoryselectedlistener listener) { dirlistenerlist.remove(listener); } /** * show file dialog */ public void showdialog() { createfiledialog().show(); } private void firefileselectedevent(final file file) { filelistenerlist.fireevent(new listenerlist.firehandler<fileselectedlistener>() { public void fireevent(fileselectedlistener listener) { listener.fileselected(file); } }); } private void firedirectoryselectedevent(final file directory) { dirlistenerlist.fireevent(new listenerlist.firehandler<directoryselectedlistener>() { public void fireevent(directoryselectedlistener listener) { listener.directoryselected(directory); } }); } private void loadfilelist(file path) { this.currentpath = path; list<string> r = new arraylist<string>(); if (path.exists()) { if (path.getparentfile() != null) r.add(parent_dir); filenamefilter filter = new filenamefilter() { public boolean accept(file dir, string filename) { file sel = new file(dir, filename); if (!sel.canread()) return false; if (selectdirectoryoption) return sel.isdirectory(); else { boolean endswith = fileendswith != null ? filename.tolowercase().endswith(fileendswith) : true; return endswith || sel.isdirectory(); } } }; string[] filelist1 = path.list(filter); (string file : filelist1) { r.add(file); } } filelist = (string[]) r.toarray(new string[]{}); } private file getchosenfile(string filechosen) { if (filechosen.equals(parent_dir)) return currentpath.getparentfile(); else return new file(currentpath, filechosen); } public void setfileendswith(string fileendswith) { this.fileendswith = fileendswith != null ? fileendswith.tolowercase() : fileendswith; } } class listenerlist<l> { private list<l> listenerlist = new arraylist<l>(); public interface firehandler<l> { void fireevent(l listener); } public void add(l listener) { listenerlist.add(listener); } public void fireevent(firehandler<l> firehandler) { list<l> copy = new arraylist<l>(listenerlist); (l l : copy) { firehandler.fireevent(l); } } public void remove(l listener) { listenerlist.remove(listener); } public list<l> getlistenerlist() { return listenerlist; } }
this call in activity:
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); file mpath = new file(environment.getexternalstoragedirectory()+ "//dir//"); filedialog = new filedialog(this, mpath); filedialog.setfileendswith(".txt"); filedialog.addfilelistener(new filedialog.fileselectedlistener() { public void fileselected(file file) { log.d(getclass().getname(), "selected file " + file.tostring()); } }); filedialog.showdialog(); }
and stack trace:
java.lang.runtimeexception: unable start activity componentinfo{mainactivity} java.lang.nullpointerexception @ android.app.activitythread.performlaunchactivity(activitythread.java:2200) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2250) @ android.app.activitythread.access$800(activitythread.java:139) @ android.app.activitythread$h.handlemessage(activitythread.java:1200) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5105) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:792) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:608) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ rpstudios.com.filedialog.loadfilelist(filedialog.java:130) @ rpstudios.com.filedialog.<init>(filedialog.java:35) @ rpstudios.com.mainactivity.oncreate(mainactivity.java:22)
any suggestion welcomed
edit: nullpointerexception in line 130: for(string file : filelist1)
in loadfilelist()
method.
i think in public dialog createfiledialog() missing create()
dialog = builder.show();
should be
dialog = builder.create().show();
let's try ;)
Comments
Post a Comment