我正在使用此逻辑删除sdcard中的文件。它正在删除所有文件,但在文件删除操作期间我在日志中获得NullPointer异常。任何帮助?
我的代码是:
void DeleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory()) {
try {
for (File child : fileOrDirectory.listFiles()) {
DeleteRecursive(child);
}
} catch (Exception e) {
e.printStackTrace();
}
}
fileOrDirectory.delete();
}
这是我在删除操作期间收到的消息:
01-01 01:28:43.440: W/System.err(4183): java.lang.NullPointerException
01-01 01:28:43.450: W/System.err(4183): at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:34)
01-01 01:28:43.450: W/System.err(4183): at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183): at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183): at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183): at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183): at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183): at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183): at com.stericsson.sdcard.Delete_sdcard1Activity.DeleteRecursive(Delete_sdcard1Activity.java:35)
01-01 01:28:43.450: W/System.err(4183): at com.stericsson.sdcard.Delete_sdcard1Activity.onCreate(Delete_sdcard1Activity.java:23)
01-01 01:28:43.450: W/System.err(4183): at android.app.Activity.performCreate(Activity.java:4465)
01-01 01:28:43.450: W/System.err(4183): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-01 01:28:43.450: W/System.err(4183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
01-01 01:28:43.450: W/System.err(4183): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-01 01:28:43.450: W/System.err(4183): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-01 01:28:43.450: W/System.err(4183): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-01 01:28:43.450: W/System.err(4183): at android.os.Handler.dispatchMessage(Handler.java:99)
01-01 01:28:43.450: W/System.err(4183): at android.os.Looper.loop(Looper.java:137)
01-01 01:28:43.450: W/System.err(4183): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-01 01:28:43.450: W/System.err(4183): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 01:28:43.450: W/System.err(4183): at java.lang.reflect.Method.invoke(Method.java:511)
01-01 01:28:43.450: W/System.err(4183): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-01 01:28:43.450: W/System.err(4183): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-01 01:28:43.450: W/System.err(4183): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
如果File对象表示的路径名不是目录或者发生了一些I / O错误,则listFIles()函数可能返回null。 作为标准做法,您应该为listFiles()的返回值添加空检查。
答案 1 :(得分:1)
Apache的commons-io FileUtils
FileUtils.deleteDirectory(new File("directory"));
答案 2 :(得分:0)
以递归方式删除文件/文件夹:
public boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
答案 3 :(得分:0)
这个:if(fileOrDirectory.isDirectory())
应该是if(fileOrDirectory != null && fileOrDirectory.isDirectory())
而filesOrDirectory.delete();
应该是
if(filesOrDirectory != null) {
filesOrDirectory.delete();
}