gpt4 book ai didi

android - 调用 getFilesDir() 时崩溃

转载 作者:太空狗 更新时间:2023-10-29 12:57:02 25 4
gpt4 key购买 nike

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

........

和堆栈跟踪

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

最佳答案

你的类是否扩展了Activity?需要更多代码上下文才能获得正确答案。

编辑

你在哪里调用 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");
}
}

关于android - 调用 getFilesDir() 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5418189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com