当我的应用程序在我的 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);
(第一个功能)
我不明白为什么这只发生在一台设备上!
编辑1:
目录声明:
directory = new File("/data/log");
(它已经定义为全局变量)