Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.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_Android Layout_Android Studio_Sharedpreferences - Fatal编程技术网

Java 高分不是';他没有得救

Java 高分不是';他没有得救,java,android,android-layout,android-studio,sharedpreferences,Java,Android,Android Layout,Android Studio,Sharedpreferences,我试着用这个问题的答案来保存一个简单的int,score,但是我遇到了麻烦。从我的游戏活动中,我传递了一个名为newScore的int,并将其放在我的丢屏活动中。我检查newScore>highScore,如果是,我将newScore作为新的高分。我还显示了这两个。目前,它总是在屏幕上显示高分和相同的分数。所以即使我得了10分,下一次如果我回来得了1分,我会把1分作为高分 这是我尝试访问高分的代码,如果高分更大,则输入新分数 从` public class LoseScreen extends

我试着用这个问题的答案来保存一个简单的int,score,但是我遇到了麻烦。从我的游戏活动中,我传递了一个名为newScore的int,并将其放在我的丢屏活动中。我检查newScore>highScore,如果是,我将newScore作为新的高分。我还显示了这两个。目前,它总是在屏幕上显示高分和相同的分数。所以即使我得了10分,下一次如果我回来得了1分,我会把1分作为高分

这是我尝试访问高分的代码,如果高分更大,则输入新分数

从`

public class LoseScreen extends AppCompatActivity`:

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

    Bundle b = getIntent().getExtras();
    int newScore = b.getInt("newScore");

    TextView textView6 = (TextView) findViewById(R.id.textView6);
    textView6.setText("Score: " + newScore);

    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
    int highScore = prefs.getInt("highScore", 0); //0 is the default value

    if (newScore > highScore) {
        prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt("highScore", newScore);
        editor.commit();
        highScore = newScore;
    }

    TextView textView8 = (TextView) findViewById(R.id.textView8);
    textView8.setText("High Score: " + highScore);
}
我还将在我的主要游戏活动中加入我的退出方法;我不会发布我的类的全部代码,因为我认为它相当大而且不相关。如果有帮助的话,我很乐意贴出来

 public void quit() {
    Intent intent = new Intent(ColorMatch2.this,
            LoseScreen.class);
    int newScore = score;
    Bundle b = new Bundle();
    b.putInt("newScore", newScore); // Your score
    intent.putExtras(b); // Put your score to your next
    startActivity(intent);
}

这很奇怪,但是没有改变代码,它现在可以工作了。发布这个问题后,我的android工作室开始出问题;我遇到了一个无法解决符号“r”的错误,必须重建并清理我的项目。重新启动几次后,我注意到高分现在也被正确保存。奇怪。

所以这很奇怪,但在不更改代码的情况下,它现在可以工作了。发布这个问题后,我的android工作室开始出问题;我遇到了一个无法解决符号“r”的错误,必须重建并清理我的项目。重新启动几次后,我注意到高分现在也被正确保存。奇怪。

你是如何挽救自己的分数的?问题在哪里?(在你的应用程序中存储数据基本上有三种方法:a)共享参考b)文件c)数据库如果你知道你想要哪种方法,请告诉我你可以得到什么帮助!(看看SharedReferences!)我试过SharedReferences,它在第一个叫做lose screen的块中。这部分:SharedReferences prefs=this.getSharedReferences(“MyPrefsky”,Context.MODE_PRIVATE);int highScore=prefs.getInt(“highScore”,0)//如果(newScore>highScore){prefs=this.getSharedReferences(“myPrefsKey”,Context.MODE_PRIVATE);SharedReferences.Editor=prefs.edit();Editor.putin(“highScore”,newScore);Editor.commit();highScore=newScore;}很难在这里读到评论,但在第一个区块大约有20行,从共享首选项prefs=uhm开始,我没有看到!对不起,我错了!没关系,很容易错过。你能看出我做错了什么吗?我相信这是一个简单的错误,但我是一个安卓noob,不知道为什么它不能正确保存高分。你试图保存你的分数是什么?问题在哪里?(在你的应用程序中存储数据基本上有三种方法:a)共享参考b)文件c)数据库如果你知道你想要哪种方法,请告诉我你可以得到什么帮助!(看看SharedReferences!)我试过SharedReferences,它在第一个叫做lose screen的块中。这部分:SharedReferences prefs=this.getSharedReferences(“MyPrefsky”,Context.MODE_PRIVATE);int highScore=prefs.getInt(“highScore”,0)//如果(newScore>highScore){prefs=this.getSharedReferences(“myPrefsKey”,Context.MODE_PRIVATE);SharedReferences.Editor=prefs.edit();Editor.putin(“highScore”,newScore);Editor.commit();highScore=newScore;}很难在这里读到评论,但在第一个区块大约有20行,从共享首选项prefs=uhm开始,我没有看到!对不起,我错了!没关系,很容易错过。你能看出我做错了什么吗?我相信这是一个简单的错误,但我是一个安卓noob,不知道为什么它不能正确保存高分。好吧,没那么糟糕。。。这样做比它仍然不运行要好=)好吧-没那么糟糕。。。这样做比它仍然不运行要好=)