我知道 getDir 将创建一个目录(如果该目录尚不存在)。但我崩溃了:
File dir= getDir(file,Context.MODE_PRIVATE);
文件的类型为字符串。 logcat 显示:
12-29 21:56:09.456: ERROR/AndroidRuntime(631): java.lang.NullPointerException
12-29 21:56:09.456: ERROR/AndroidRuntime(631): at android.content.ContextWrapper.getDir(ContextWrapper.java:198)
代码如下:
public class ReportHome extends Application{
public ReportHome(String message, String file){
…some code…
inf(tosend,file);
}
public void inf(String c, String file){
File dir= getDir(file,Context.MODE_PRIVATE);
顺便说一句:正确格式化多行 logcat 的技巧是什么?
最佳答案
您确定file
参数不为空吗?
如果不是,那么另一种可能性是您没有在正常的环境中运行它。
查看source code for getDir() ,唯一的另一种可能性是 mBase
(保存当前 Context
的私有(private)变量)为 null。如果没有看到您的其余代码,我不太确定会发生什么。
关于java - 为什么我在 getDir 上收到 NullPointerException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8673567/