getFilesDir()返回null

Squ*_*mmy 15 android

GetFilesDir()返回null,这完全是奇怪的.更糟糕的是,我的代码是......非常简单:

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));
}
Run Code Online (Sandbox Code Playgroud)

这将在f.getAbsolutePath()上崩溃.它目前在模拟器中运行,在1024x600下模拟2.3.3系统.

谷歌在这方面什么也没有回报......那么为什么地球上会发生这种情况?我该如何修复它?!

非常感谢!

编辑::已解决!

嗯,有点......

我认为这是一个模拟器错误.我将模拟器的外观更改为WXVGA,然后允许它正确地写入文件.然后,当我将分辨率更改回1024x600时,它保持正常工作.

所以,如果有人有类似的错误,我建议你使用模拟器,直到找到超越错误的方法.

A. *_*iri 0

尝试在 f.getAbsolutePath() 之前添加此内容:

File f = new File(this.getApplicationContext().getFilesDir() + "/");
f.mkdirs();
Run Code Online (Sandbox Code Playgroud)