我想从后台服务在应用程序文件夹中创建目录。我已经使用 getDir 创建了一个目录。我正在向 DataManager 发送服务上下文以创建目录。此外,我已经确认我没有收到空上下文。但我在“ ctx.getDir("Application_Name", Context.MODE_PRIVATE)
”线上崩溃了。
private File systemDir;
private File dataDir;
public DataManager(Context ctx) {
try{
Log.e("xlogger", "DataManager Context : "+ctx);
systemDir = ctx.getDir("Application_Name", Context.MODE_PRIVATE );
dataDir = new File(systemDir, "Data/");
if(!dataDir.exists()){
dataDir.mkdir();
}
}catch (Exception e) {
Log.e("xlogger", "DataManager Exception : "+Log.getStackTraceString(e));
}
}
崩溃报告:
02-27 13:46:13.100: D/xlogger(4762): DataManager Context = com.MyApp.engine.MyService@4055ad38
02-27 13:46:13.110: E/xlogger(4762): DataManager Exception : java.lang.NullPointerException
02-27 13:46:13.110: E/xlogger(4762): at android.content.ContextWrapper.getDir(ContextWrapper.java:198)
02-27 13:46:13.110: E/xlogger(4762): at com.MyApp.utils.DataManager.<init>(DataManager.java:30)
02-27 13:46:13.110: E/xlogger(4762): at com.MyApp.communication.MyAppClient.getRulesXML(MyAppClient.java:867)
02-27 13:46:13.110: E/xlogger(4762): at com.MyApp.engine.MyService$5.doInBackground(MyService.java:862)
02-27 13:46:13.110: E/xlogger(4762): at com.MyApp.engine.MyService$5.doInBackground(MyService.java:1)
02-27 13:46:13.110: E/xlogger(4762): at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-27 13:46:13.110: E/xlogger(4762): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-27 13:46:13.110: E/xlogger(4762): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-27 13:46:13.110: E/xlogger(4762): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-27 13:46:13.110: E/xlogger(4762): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-27 13:46:13.110: E/xlogger(4762): at java.lang.Thread.run(Thread.java:1019)
帮助高度赞赏。谢谢。