我有一个录制音频的应用。我正在尝试获取存储在SD卡上的文件名,以便我可以将它们放在列表视图中。我知道在sd卡的根目录保存文件可能不是最佳做法,但我有:)(与dcim相同的水平,声音等)。我得到了关于listFiles()方法的nullpointerexception。这是我的代码。关于例外的任何想法?
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)
答案 0 :(得分:1)
你得到一个NPE,可能在这里:
Log.i("arr******", ""+arr.size());
您是否首先初始化该列表?