简体   繁体   English

Android不创建/读取文件

[英]Android Not Creating/Reading File

Disclaimer: I've went over many examples and read the docs. 免责声明:我已经阅读了许多示例并阅读了文档。

This question entails 3 classes. 这个问题需要3个班级。 My serialized class, a helper class that handles I/O, and an activity for interfacing. 我的序列化类,处理I / O的辅助类以及用于接口的活动。

My serialized class looks something like this: 我的序列化类如下所示:

public class EntityPlayer implements Serializable {

private static final long serialVersionUID = -7671612522335708108L;
String name = "foo";

public EntityPlayer() {
    name = "foo";

}

} }

My helper class looks like this (this is where the heavy lifting is done): 我的辅助程序类如下(这是繁重的工作):

public class Main {

static Object loadPlayer(Context c, String name) throws IOException,
        ClassNotFoundException {
    FileInputStream f = c.openFileInput(name);
    ObjectInputStream i = new ObjectInputStream(f);
    Object player = i.readObject();
    i.close();
    return player;
}

static void savePlayer(Context c, String name, EntityPlayer player) throws IOException {
    FileOutputStream f = c.openFileOutput(name, Context.MODE_PRIVATE);
    ObjectOutputStream o = new ObjectOutputStream(f);
    o.writeObject(player);
    o.flush();
    o.close();
}

} }

and then finally, this is how I'm accesing (in a nut shell): 最后,这就是我的访问方式(在坚果壳中):

public class DisplayInteract extends Activity implements OnClickListener {

EntityPlayer player = new EntityPlayer();  //by editing this line, my issue was fixed


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.g_interact);

    player = new EntityPlayer(ID, this);

    try {
        Main.savePlayer(player.name, player);
    } catch (IOException e) {
    }

    try {
        player = (EntityPlayer) Main.loadPlayer("Derp");
    } catch (ClassNotFoundException e) {
    } catch (IOException e) {
    }
}

} }

Basically I'm getting a NPE when I make any calls concerning the serialized class after loading it. 基本上,在加载序列化类后进行任何调用时,都会得到一个NPE。 I know the issue is in the way that I'm saving the file because I can tell that there is no file through the Android file explorer in eclipse. 我知道问题在于保存文件的方式,因为我可以通过eclipse中的Android文件资源管理器判断没有文件。 I know the problem also is in how I'm loading the file because (I have no ideal how) I got it to work at one point (it created a file) but I still couldn't access any of the data without it throwing an NPE (because I wasn't accessing the class correctly) 我知道问题也出在我如何加载文件上,因为(我不怎么理想)我在某一时刻使它可以工作(它创建了一个文件),但是我仍然无法访问任何数据而没有抛出它NPE(因为我没有正确访问课程)

You're not specifying a path where the file should be saved to. 您未指定文件应保存到的路径。 Use the openFileOutput() from Context which will save it to your applications data folder. 使用Context中的openFileOutput()将其保存到您的应用程序数据文件夹中。

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);

I don't know how you're generating a filename, but you might want to make sure you have storage available (free space/not in use by usb mass storage/etc.). 我不知道您是如何生成文件名的,但是您可能要确保有可用的存储空间(可用空间/ usb大容量存储空间未使用等)。 Try something along the following lines: 尝试以下方法:

    //Is external storage available?
    String state = Environment.getExternalStorageState();
    if (!Environment.MEDIA_MOUNTED.equals(state))
    {
        //If we can't get storage, let the user know
        return;
    }

    //Create the actual file
    //The file should end up in /mnt/sdcard/Android/data/your.package.name/files/
    File dir = new File(Environment.getExternalStorageDirectory() + EXTERNAL_FILES_DIR_EXTENTION);

    //Make all needed directories if they do not exist
    dir.mkdirs();

    //Create the file
    File file = new File(dir, fileName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM