gpt4 book ai didi

java - getExternalFilesDir(null) 抛出 NullPointerException

转载 作者:太空狗 更新时间:2023-10-29 15:42:40 34 4
gpt4 key购买 nike

我正在构建一个 Android 应用程序,我选择在外部存储中使用 flat flies 作为我的持久化方法。我有一个使用一些静态实例方法扩展 Application 的类。调试几次后,我注意到没有一个实例变量是 nullNullPointerException 来自 Context.getExternalFilesDir(null).

我想在任何答案前加上它不返回 null。此外,我的 list 中有正确的权限,并检查 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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com