调用getFilesDir()时崩溃

时间:2011-03-24 11:09:54

标签: android

调用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

2 个答案:

答案 0 :(得分:2)

你的课程是否扩展了活动?需要更多的代码上下文来获得正确的答案。

修改

你在哪里调用generateFile()?在应用程序完全加载之前,您可能会要求FilesDirectory。

你只需要做一些检查:

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");
 }
}

答案 1 :(得分:1)

OP没有提供足够的信息以便能够提出解决方案,但我在同一行中遇到了类似的问题,我将调用转移到包括getFilesDir()的方法。在定义MyActivity字段之前调用之前,在我将其移动到该活动的onCreate()之后,异常就消失了。