Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 关闭应用程序后保存一个变量_Java_Android_View_Save - Fatal编程技术网

Java 关闭应用程序后保存一个变量

Java 关闭应用程序后保存一个变量,java,android,view,save,Java,Android,View,Save,我有一个保存变量值的问题 (安卓) 我有一个扩展视图的activity,但我不知道如何在扩展视图的activity中使用save和read from txt文件,我尝试序列化和反序列化从主activity调用的其他类。但是没有跑步。 不允许我调用序列化和反序列化的方法 我需要从应用程序保存Higscore。如果我重新启动应用程序,我需要知道你的分数 public class Game extends View { 我需要把这个值赋予游戏类。但我不知道怎么做 谢谢你的建议 拯救 .getAc

我有一个保存变量值的问题

(安卓)

我有一个扩展视图的activity,但我不知道如何在扩展视图的activity中使用save和read from txt文件,我尝试序列化和反序列化从主activity调用的其他类。但是没有跑步。 不允许我调用序列化和反序列化的方法

我需要从应用程序保存Higscore。如果我重新启动应用程序,我需要知道你的分数

public class Game extends View  {
我需要把这个值赋予游戏类。但我不知道怎么做

谢谢你的建议


拯救

.getActivity()具有红色。Compiler不知道这种方法

阅读

.getActivity()具有红色。Compiler也不知道这种方法 和 getContext().getString((R.string.saved_high_score),defaultValue)位于地下
(SharedReferences中的getInt(string,int)不能应用于(string))

这应该适用于您:

保存值:

SharedPreferences sharedPref =
getContext().getSharedPreferences("preferences",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("highscore", [here goes your highscore]);
editor.commit();
读取值:

getContext().getSharedPreferences("preferences",Context.MODE_PRIVATE);
    int defaultValue = 0;
    long highScore = sharedPref.getInt("highscore", defaultValue);

Android为您提供了几个保存持久应用程序数据的选项。检查一下我的活动中没有一次创建,因为每50分钟我只有画布和所有颜料。我尝试使用您的代码,但是。getActivity和getString错误:(933,17)错误:找不到符号方法getActivity()错误:(936,23)错误:找不到符号方法getString(int)错误:(939,41)错误:找不到符号方法getActivity()错误:(941,44)错误:找不到符号方法getString(int),我已经在答案中说明了这一点。使用getContext()代替getActivity()使用getContext()和getString(…)使用getContext()。getString(…)可以从上下文中获取这些内容,也可以是活动。这是基础知识,所以您应该从一些基础知识开始。getContext().getActivity()不运行。get活动仍然是红色的。只需使用getContext()。getPreferences(…)我编辑了答案。你真的应该回到基础上来。
SharedPreferences sharedPref =
getContext().getSharedPreferences("preferences",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("highscore", [here goes your highscore]);
editor.commit();
getContext().getSharedPreferences("preferences",Context.MODE_PRIVATE);
    int defaultValue = 0;
    long highScore = sharedPref.getInt("highscore", defaultValue);