List files present in a directory android -


i'm trying display list of files present in directory in smartphone. code i'm dealing with:

public class mainactivity extends activity {  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);       final button button = (button) findviewbyid(r.id.button1);      button.setonclicklistener(new view.onclicklistener() {         public void onclick(view v) {           file f = new file(environment.getexternalstoragedirectory().tostring()+"/directory_movies/team_15");          log.d("files", "path: " +environment.getexternalstoragedirectory().tostring()+"/directory_movies/team_15");         file file[] = f.listfiles();         log.d("files", "size: "+ file.length);         (int i=0; < file.length; i++)         {             log.d("files", "filename:" + file[i].getname());         }            }      }); } 

so when press button should see in logcat files present in folder: /directory_movies/team_15 appears:

05-02 18:02:08.789: e/androidruntime(2616): fatal exception: main 05-02 18:02:08.789: e/androidruntime(2616): java.lang.nullpointerexception 05-02 18:02:08.789: e/androidruntime(2616):     @ com.example.list.mainactivity$1.onclick(mainactivity.java:32) 05-02 18:02:08.789: e/androidruntime(2616):     @ android.view.view.performclick(view.java:2506) 05-02 18:02:08.789: e/androidruntime(2616):     @ android.view.view$performclick.run(view.java:9112) 05-02 18:02:08.789: e/androidruntime(2616):     @ android.os.handler.handlecallback(handler.java:587) 05-02 18:02:08.789: e/androidruntime(2616):     @ android.os.handler.dispatchmessage(handler.java:92) 05-02 18:02:08.789: e/androidruntime(2616):     @ android.os.looper.loop(looper.java:130) 05-02 18:02:08.789: e/androidruntime(2616):     @ android.app.activitythread.main(activitythread.java:3835) 05-02 18:02:08.789: e/androidruntime(2616):     @ java.lang.reflect.method.invokenative(native method) 05-02 18:02:08.789: e/androidruntime(2616):     @ java.lang.reflect.method.invoke(method.java:507) 05-02 18:02:08.789: e/androidruntime(2616):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:864) 05-02 18:02:08.789: e/androidruntime(2616):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:622) 05-02 18:02:08.789: e/androidruntime(2616):     @ dalvik.system.nativestart.main(native method) 05-02 18:07:08.812: i/process(2616): sending signal. pid: 2616 sig: 9 

what wrong??

in case, environment.getexternalstoragedirectory() returns null. , try access .tostring() of null variable.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -