getExternalFilesDir(null)抛出NullPointerException

时间:2012-10-11 01:17:49

标签: java android nullpointerexception

我正在构建一个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" />

非常感谢任何帮助。我已经在这几个小时内苦苦挣扎,我会接受任何建议。谢谢

编辑: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)

1 个答案:

答案 0 :(得分:10)

您正在构造函数中进行初始化;这不行。你应该在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吗?)