调用getfilesdir()时崩溃,下面是代码

public void generateFile() throws IOException{


    File fileDir = getFilesDir(); //crashing

    String filedir=fileDir.toString();

    String OUTPUT_FILE = "123.txt";
    String data = "TEST DATA to WRITE";

    File newFile = new File(fileDir,OUTPUT_FILE);

    ........

和stacktrace
03-24 16:33:33.623: VERBOSE/APP(2180): Calling GenerateFile
03-24 16:33:33.623: DEBUG/AndroidRuntime(2180): Shutting down VM
03-24 16:33:33.623: WARN/dalvikvm(2180): threadid=1: thread exiting with uncaught exception (group=0x4001d8a8)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180): FATAL EXCEPTION: main
03-24 16:33:33.670: ERROR/AndroidRuntime(2180): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Android.APP/com.Android.APP.Readings}: java.lang.NullPointerException
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.widget.TabHost.setCurrentTab(TabHost.java:326)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.view.View.performClick(View.java:2408)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.view.View$PerformClick.run(View.java:8818)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.os.Handler.handleCallback(Handler.java:587)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.os.Looper.loop(Looper.java:123)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at java.lang.reflect.Method.invokeNative(Native Method)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at java.lang.reflect.Method.invoke(Method.java:521)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at dalvik.system.NativeStart.main(Native Method)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180): Caused by: java.lang.NullPointerException
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.content.ContextWrapper.getFilesDir(ContextWrapper.java:178)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at com.Android.APP.Logging.generateFile(Logging.java:33)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at com.Android.APP.Readings.onCreate(Readings.java:90)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-24 16:33:33.670: ERROR/AndroidRuntime(2180):     ... 18 more

最佳答案

你们班延长活动时间吗?需要更多的代码上下文才能得到正确的答案。
编辑
你在哪里调用generateFile()?在应用程序完全加载之前,您可能需要文件目录。
你只需要做些检查:

public void generateFile() throws IOException{

 File fileDir = getFilesDir(); //crashing
 if(fileDir != null){
   String filedir=fileDir.toString();

   String OUTPUT_FILE = "123.txt";
   String data = "TEST DATA to WRITE";

   File newFile = new File(fileDir,OUTPUT_FILE);
 } else {
    Log.e("MyApp", "Something went wrong, filesDir is null");
 }
}

10-02 08:59