我正在構建一個Android應用程序,並且我選擇在外部存儲中使用扁平的蒼蠅用於我的持久性方法。我有一個類擴展了應用程序與一些靜態實例方法。經過幾次調試後,我注意到沒有一個實例變量是null
,而NullPointerException
來自Context.getExternalFilesDir(null)
。getExternalFilesDir(null)拋出NullPointerException
我想以任何答案作爲前言,它不返回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)
你能後的logcat的輸出? (不要只是吞下事件,獲取堆棧跟蹤。) – Eric
另外,您可能希望將'READ_EXTERNAL_STORAGE'權限添加到清單。 – Eric