我正在构建一个 Android 应用程序,并且我选择在外部存储中使用扁平苍蝇作为我的持久性方法。我有一个使用一些静态实例方法扩展 Application 的类。调试几次后,我注意到实例变量都不是null
,并且NullPointerException
来自内部Context.getExternalFilesDir(null)
。
我想以它不返回null作为任何答案的开头。此外,我在清单中拥有正确的权限,并检查 SD 卡是否已安装到设备上。
这是保存调用getExternalFilesDir(null)
并引发错误的类。
public class WhileImOut extends Application {
public static TaskManager taskManager;
private static Context appContext;
public WhileImOut() {
super();
appContext = this;
initialize();
}
public static void initialize() {
if (taskManager == null && Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
try{
File f = appContext.getExternalFilesDir(null); // Exception being thrown here
taskManager = new TaskManager(f.getAbsolutePath());
}catch(NullPointerException e){
Log.d("Bad","NPE");
}catch(Exception e){
}
}
}
}
这是在我的 AndroidManifest.xml 中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
任何帮助将不胜感激。我已经为此苦苦挣扎了几个小时,我会接受给出的任何建议。谢谢
EDIT: LogCat information
10-10 18:55:56.424: E/AndroidRuntime(964): FATAL EXCEPTION: main
10-10 18:55:56.424: E/AndroidRuntime(964): java.lang.RuntimeException: Unable to resume activity {zsmith.capstone.whileimout/zsmith.capstone.whileimout.TaskListActivity}: java.lang.NullPointerException
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.os.Looper.loop(Looper.java:137)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-10 18:55:56.424: E/AndroidRuntime(964): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 18:55:56.424: E/AndroidRuntime(964): at java.lang.reflect.Method.invoke(Method.java:511)
10-10 18:55:56.424: E/AndroidRuntime(964): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-10 18:55:56.424: E/AndroidRuntime(964): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-10 18:55:56.424: E/AndroidRuntime(964): at dalvik.system.NativeStart.main(Native Method)
10-10 18:55:56.424: E/AndroidRuntime(964): Caused by: java.lang.NullPointerException
10-10 18:55:56.424: E/AndroidRuntime(964): at zsmith.capstone.whileimout.TaskListActivity.onResume(TaskListActivity.java:58)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.Activity.performResume(Activity.java:5082)
10-10 18:55:56.424: E/AndroidRuntime(964): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)