Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java getExternalFilesDir(null)引发NullPointerException_Java_Android_Nullpointerexception - Fatal编程技术网

Java getExternalFilesDir(null)引发NullPointerException

Java getExternalFilesDir(null)引发NullPointerException,java,android,nullpointerexception,Java,Android,Nullpointerexception,我正在构建一个Android应用程序,我选择在外部存储中使用flat flies作为我的持久化方法。我有一个类,它使用一些静态实例方法扩展应用程序。调试几次之后,我注意到没有一个实例变量是null,而NullPointerException来自Context.getExternalFilesDir(null) 我想在任何答案的前面加上它不返回null。此外,我在清单中拥有正确的权限,并检查SD卡是否已安装到设备上 这个类保存对getExternalFilesDir(null)的调用并抛出错误 p

我正在构建一个Android应用程序,我选择在外部存储中使用flat flies作为我的持久化方法。我有一个类,它使用一些静态实例方法扩展应用程序。调试几次之后,我注意到没有一个实例变量是
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" />

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

是否可以发布LogCat输出?(不要只是吞下事件,获取堆栈跟踪。)此外,您可能需要将
读取外部存储
权限添加到清单中。啊。。。我的朋友向我展示了如何做到这一点,他说onCreate在这种情况下不是一个有效的方法,所以我没有真正检查它。非常感谢你。它成功了,现在我可以继续前进了。到目前为止,还没有理由,但我计划在我的主要问题得到解决后解决问题。Simular Gos for Service。在构造函数中,getExternalFilesDir(null)抛出NPE。尽管对于getExternalFilesDir(null),回调onHandleContent是可以的。
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){

            }
        }
    }
}