当我的应用程序在我的S2上运行时,它完全没有错误,但是当我尝试在我的Nexus 7上运行时,它会关闭,我不知道它为什么会在一台设备上发生,而不是在另一台设备上。
这是LogCat:
07-03 18:33:29.139: E/AndroidRuntime(11990): FATAL EXCEPTION: main
07-03 18:33:29.139: E/AndroidRuntime(11990): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.liamw.root.logeraser/com.liamw.root.logeraser.MainActivity}: java.lang.NullPointerException
07-03 18:33:29.139: E/AndroidRuntime(11990): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2312)
07-03 18:33:29.139: E/AndroidRuntime(11990): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
07-03 18:33:29.139: E/AndroidRuntime(11990): at android.app.ActivityThread.access$600(ActivityThread.java:156)
07-03 18:33:29.139: E/AndroidRuntime(11990): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1250)
07-03 18:33:29.139: E/AndroidRuntime(11990): at android.os.Handler.dispatchMessage(Handler.java:99)
07-03 18:33:29.139: E/AndroidRuntime(11990): at android.os.Looper.loop(Looper.java:137)
07-03 18:33:29.139: E/AndroidRuntime(11990): at android.app.ActivityThread.main(ActivityThread.java:5229)
07-03 18:33:29.139: E/AndroidRuntime(11990): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 18:33:29.139: E/AndroidRuntime(11990): at java.lang.reflect.Method.invoke(Method.java:525)
07-03 18:33:29.139: E/AndroidRuntime(11990): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
07-03 18:33:29.139: E/AndroidRuntime(11990): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
07-03 18:33:29.139: E/AndroidRuntime(11990): at dalvik.system.NativeStart.main(Native Method)
07-03 18:33:29.139: E/AndroidRuntime(11990): Caused by: java.lang.NullPointerException
07-03 18:33:29.139: E/AndroidRuntime(11990): at com.liamw.root.logeraser.FolderTools.folderSize(FolderTools.java:96)
07-03 18:33:29.139: E/AndroidRuntime(11990): at com.liamw.root.logeraser.FolderTools.folderSize(FolderTools.java:87)
07-03 18:33:29.139: E/AndroidRuntime(11990): at com.liamw.root.logeraser.MainActivity.initialize(MainActivity.java:194)
07-03 18:33:29.139: E/AndroidRuntime(11990): at com.liamw.root.logeraser.MainActivity.onCreate(MainActivity.java:131)
07-03 18:33:29.139: E/AndroidRuntime(11990): at android.app.Activity.performCreate(Activity.java:5167)
07-03 18:33:29.139: E/AndroidRuntime(11990): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-03 18:33:29.139: E/AndroidRuntime(11990): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
07-03 18:33:29.139: E/AndroidRuntime(11990): ... 11 more
2个文件夹大小功能是:
public long folderSize() {
long length = 0;
for (File file : directory.listFiles()) {
if (file.isFile())
length += file.length();
else if (file != null)
length += folderSize(file);
}
return length;
}
和
public long folderSize(File directory) {
long length = 0;
for (File file : directory.listFiles()) {
if (file.isFile())
length += file.length();
else if (file != null)
length += folderSize(file);
}
return length;
}
第96行:
for (File file : directory.listFiles()) {
(第二功能)
第87行:
length += folderSize(file);
(第一个功能)
我不明白为什么这只会在一台设备上发生!
EDIT1:
目录声明:
directory = new File("/data/log");
(它已被定义为全局变量)
答案 0 :(得分:2)
目录不存在于Nexus 7上,所以directory.listFiles()返回null,因此迭代器会抛出一个NPE。
这里是File.listFiles()的javadoc所说的内容:
An array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
一个简单的exists()可以完成这项工作,但它不会涵盖您无法访问该目录的情况,该目录也将返回Null。所以最好只检查listFiles()返回null并在执行此操作时将两个方法合并为一个:
public long folderSize(File directory) {
long length = 0;
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
length += file.isFile() ? file.length() : folderSize(file);
}
}
return length;
}
现在你只需要调用它来传递目录:
long size = folderSize(directory);
还要确保您在清单中定义了android.permission.READ_EXTERNAL_STORAGE权限。