0
我有一個應用程序可以記錄音頻。我試圖得到存儲在SD卡上的文件名,所以我可以把它們放在一個列表視圖中。我知道這可能不是最好的做法來保存文件在SD卡的根目錄,但我有:)(與dcim相同的級別,聽起來像)。我得到關於listFiles()方法的nullpointerexception。這是我的代碼。關於例外的任何想法?無法從SD卡獲取文件名
File sdCardRoot = Environment.getExternalStorageDirectory();
Log.i("root on sd =", ""+ sdCardRoot.getPath());
for (File f : sdCardRoot.listFiles()) {
if (f.isFile()){
String name = f.getName();
Log.i("arr names******", ""+arr.get(0));
arr.add(name);
}else
{
Log.i("file", " no file");
}
Log.i("arr******", ""+arr.size());
}
02-13 19:16:19.063: INFO/ActivityManager(1903): Starting activity: Intent { cmp=com.tecmark/.SdGetList }
02-13 19:16:19.118: INFO/root on sd =(10308): /sdcard
02-13 19:16:19.123: INFO/file(10308): no file
02-13 19:16:19.128: DEBUG/AndroidRuntime(10308): Shutting down VM
02-13 19:16:19.128: WARN/dalvikvm(10308): threadid=3: thread exiting with uncaught exception (group=0x4001b180)
02-13 19:16:19.128: ERROR/AndroidRuntime(10308): Uncaught handler: thread main exiting due to uncaught exception
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tecmark/com.tecmark.SdGetList}: java.lang.NullPointerException
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at android.os.Handler.dispatchMessage(Handler.java:99)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at android.os.Looper.loop(Looper.java:123)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at java.lang.reflect.Method.invokeNative(Native Method)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at java.lang.reflect.Method.invoke(Method.java:521)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at dalvik.system.NativeStart.main(Native Method)
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): Caused by: java.lang.NullPointerException
02-13 19:16:19.138: ERROR/AndroidRuntime(10308): at com.tecmark.SdGetList.onCreate(SdGetList.java:38)
我正要說同樣的話。當然,你想在將文件名添加到列表之後進行登錄? IE:arr.add(name); Log.i(「arr names ******」,「」+ arr.get(0));另外獲得(0)只會顯示第一個文件,這需要增加。剛剛更改爲「」+名稱 – stealthcopter 2011-02-13 20:03:03