我正在构建一个android应用程序,我选择在外部存储器中使用flatfiles作为持久化方法。我有一个用一些静态实例方法扩展应用程序的类。在调试了几次之后,我注意到没有一个实例变量是null,并且NullPointerException来自Context.getExternalFilesDir(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" />

任何帮助都将不胜感激。我已经挣扎了几个小时,我会接受任何建议。谢谢你
编辑:logcat信息
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)

最佳答案

您正在构造函数中进行初始化;这将不起作用。您应该在onCreate()中进行初始化。调用构造函数时,未正确初始化上下文。

public class WhileImOut extends Application {

    public static TaskManager taskManager;
    private static Context appContext;

    public void onCreate() {
        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){

            }
        }
    }
}

(顺便问一下,有没有理由单独使用initialize()方法?如果是的话,是否有任何原因使其public

关于java - getExternalFilesDir(null)引发NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12830827/

10-10 11:19