getFilesDir()返回nullgetFilesDir、null

2023-09-07 15:23:30 作者:败给感情

GetFilesDir()将返回null,这完全是离奇。更糟的是,是我的code是...痛苦简单:

GetFilesDir() is returning null, which is completely bizarre. What's worse, is my code is...painfully simple:

public class main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    File f = this.getFilesDir();
    f.getAbsolutePath();

    tempClicked((ToggleButton)findViewById(R.id.tempToggle));
}

这会崩溃的f.getAbsolutePath()。它目前在模拟器中运行在1024x600的一个模拟系统2.3.3。

This will crash on f.getAbsolutePath(). It's currently running in the emulator emulating a 2.3.3 system at 1024x600.

谷歌返回这个没什么...那么,为什么在地球上会出现这种情况?如何解决呢?!

Google returns nothing on this...so why on earth is this happening? And how do I fix it?!

非常感谢!

修改::解决了!

嘛,有点...

我想到这竟然是一个模拟器错误。我改变了仿真器的皮肤WXVGA,并再允许它正确地写入文件。而当我再变分辨率回1024x600的它一直正常工作。

I think it turned out to be an emulator error. I changed the skin of the emulator to WXVGA, and that then allowed it to write the files correctly. And when I then changed the resolution back to 1024x600 it kept working correctly.

因此​​,如果任何人有类似的错误,我建议你,直到你找到一个方法过去的错误与模拟器玩。

So, if anyone has a similar error, I suggest you play around with the emulator until you find a way past the bug.

推荐答案

尝试f.getAbsolutePath()之前加入这样的:

Try adding this before f.getAbsolutePath():

File f = new File(this.getApplicationContext().getFilesDir() + "/");
f.mkdirs();