Android-内部存储读/写问题
我正在开发一款Android游戏。我有一个扩展视图的游戏类和一个主活动类 我正在尝试从内部存储器加载高分。我希望它加载到onCreate()中并保存在onDestroy()中 我正在DroidX上测试应用程序。它在尝试读取文件时在启动时崩溃。如果我注释掉该部分来阅读它,应用程序将正常运行,并按其应该的方式写入数据。如果在保存数据时再次运行,则会正确加载数据 在尝试加载文件之前,如何检查文件是否存在 感谢advace切换到使用,它是为这个用例设计的 下面是一个示例,说明它是多么简单:Android-内部存储读/写问题,android,file,input,Android,File,Input,我正在开发一款Android游戏。我有一个扩展视图的游戏类和一个主活动类 我正在尝试从内部存储器加载高分。我希望它加载到onCreate()中并保存在onDestroy()中 我正在DroidX上测试应用程序。它在尝试读取文件时在启动时崩溃。如果我注释掉该部分来阅读它,应用程序将正常运行,并按其应该的方式写入数据。如果在保存数据时再次运行,则会正确加载数据 在尝试加载文件之前,如何检查文件是否存在 感谢advace切换到使用,它是为这个用例设计的 下面是一个示例,说明它是多么简单: public
public class Example extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
// Get highScore if it exists, otherwise default to zero
int highScore = settings.getInt("highScore", 0);
setHighScore(highScore);
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("highScore", mHighScore);
// Commit the edits!
editor.commit();
}
}
public class Example extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
// Get highScore if it exists, otherwise default to zero
int highScore = settings.getInt("highScore", 0);
setHighScore(highScore);
}
@Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("highScore", mHighScore);
// Commit the edits!
editor.commit();
}
}