您好我正在使用模拟器并且我已经创建了一个SD卡。我能够从/向SD卡读取和写入数据但是当我尝试使用Listview列出我文件夹中的文件时。我得到异常消息我确定文件夹存在是因为我可以在DDMS中看到它。
这些是文件夹文件名
private String filename = "MySampleFile.txt";
private String filepath = "MyFileStorage";
存储/ SD卡/ Android设备/数据/包/文件/
此外,正如我上面提到的,在阅读和写作时没有错。我认为路径错误
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
File root=
new File(Environment.getExternalStorageDirectory().getPath()+"/MyFileStorage");
listDir(root);
ArrayAdapter<String>adapter=
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,fileList);
this.setListAdapter(adapter);
}
private void listDir(File f) {
File[]files=f.listFiles();
for (File file : files) {
fileList.add(file.getPath());
}
}
这是例外
05-12 05:14:48.626: E/AndroidRuntime(3349): FATAL EXCEPTION: main
05-12 05:14:48.626: E/AndroidRuntime(3349): Process: com.example.read, PID: 3349
05-12 05:14:48.626: E/AndroidRuntime(3349): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.read/com.example.read.MainActivity}: java.lang.NullPointerException
05-12 05:14:48.626: E/AndroidRuntime(3349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
05-12 05:14:48.626: E/AndroidRuntime(3349): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
05-12 05:14:48.626: E/AndroidRuntime(3349): at android.app.ActivityThread.access$700(ActivityThread.java:135)
05-12 05:14:48.626: E/AndroidRuntime(3349): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
05-12 05:14:48.626: E/AndroidRuntime(3349): at android.os.Handler.dispatchMessage(Handler.java:102)
05-12 05:14:48.626: E/AndroidRuntime(3349): at android.os.Looper.loop(Looper.java:137)
05-12 05:14:48.626: E/AndroidRuntime(3349): at android.app.ActivityThread.main(ActivityThread.java:4998)
05-12 05:14:48.626: E/AndroidRuntime(3349): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 05:14:48.626: E/AndroidRuntime(3349): at java.lang.reflect.Method.invoke(Method.java:515)
05-12 05:14:48.626: E/AndroidRuntime(3349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
05-12 05:14:48.626: E/AndroidRuntime(3349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
05-12 05:14:48.626: E/AndroidRuntime(3349): at dalvik.system.NativeStart.main(Native Method)
05-12 05:14:48.626: E/AndroidRuntime(3349): Caused by: java.lang.NullPointerException
05-12 05:14:48.626: E/AndroidRuntime(3349): at com.example.read.MainActivity.listDir(MainActivity.java:32)
05-12 05:14:48.626: E/AndroidRuntime(3349): at com.example.read.MainActivity.onCreate(MainActivity.java:22)
05-12 05:14:48.626: E/AndroidRuntime(3349): at android.app.Activity.performCreate(Activity.java:5243)
05-12 05:14:48.626: E/AndroidRuntime(3349): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-12 05:14:48.626: E/AndroidRuntime(3349): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
05-12 05:14:48.626: E/AndroidRuntime(3349): ... 11 more
答案 0 :(得分:0)
您是否尝试过记录传递给list方法的文件的路径? 尝试getExternalStorageDirectory()。getAbsolutePath();