Java 如何实现SharedReferences以在会话之间保留整数?
我的应用程序运行正常,只是在关闭并重新打开应用程序后,最高分数(maxwhacks)显示为“0”(即:在会话之间没有保留)。我试图用SharedReferences修复这个bug,但现在我的应用程序在打开时崩溃了。你能找到应用程序中的错误吗?我尝试实现的两种方法是:setBestScore和getBestScore 我补充的部分是:Java 如何实现SharedReferences以在会话之间保留整数?,java,android,Java,Android,我的应用程序运行正常,只是在关闭并重新打开应用程序后,最高分数(maxwhacks)显示为“0”(即:在会话之间没有保留)。我试图用SharedReferences修复这个bug,但现在我的应用程序在打开时崩溃了。你能找到应用程序中的错误吗?我尝试实现的两种方法是:setBestScore和getBestScore 我补充的部分是: int previousMaxScore = getBestScore(); maxwhacks = previousMaxScore; TextView ma
int previousMaxScore = getBestScore();
maxwhacks = previousMaxScore;
TextView maxWhacksStored = findViewById(R.id.numberOfMaxWhacksView);
maxWhacksStored.setText(previousMaxScore);
完整代码:
爪哇:
package com.example.android.whack_a_lock_022;
导入android.content.SharedReferences;
导入android.os.Bundle;
导入android.os.CountDownTimer;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入java.util.Random;
公共类MainActivity扩展了AppCompatActivity{
public CountDownTimer CountDownTimer=null;
int cl=0;
int-nl=0;
int sl=0;
int-whacks=0;
int-maxwhacks=0;
浮点数为0到1;
随机r;
私人按钮启动;
取消专用按钮;
私有文本查看时间;
TextView maxWhacksStored;
私人共享参考myPrefs;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
maxwhacks=getBestScore();
TextView maxWhacksStored=findViewById(R.id.numberOfMaxWhacksView);
maxWhacksStored.setText(maxwhacks);
r=新随机数();
按钮clButton=findViewById(R.id.cl);
按钮nlButton=findViewById(R.id.nl);
按钮slButton=findviewbyd(R.id.sl);
View.OnClickListener btnClickListener=新建视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
开关(v.getId()){
案例R.id.启动按钮:
start();
打破
案例R.id.Cancel按钮:
取消();
打破
}
}
};
clButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(cl==1){
cl=0;
TextView CapsLock=findviewbyd(R.id.cl);
CapsLock.setText(“大写锁定”);
whacks++;
TextView NumberOfWhacks=findView-ById(R.id.numberOfWhacksView);
NumberOfWhacks.setText(String.valueOf(whacks));
if(whacks>maxwhacks){
maxwhacks=whacks;
TextView NumberOfMaxWhacks=findviewbyd(R.id.numberOfMaxWhacksView);
NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
}
pop();
}
}
});
nlButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(nl==1){
nl=0;
TextView NumLock=findviewbyd(R.id.nl);
NumLock.setText(“num lk”);
whacks++;
TextView NumberOfWhacks=findView-ById(R.id.numberOfWhacksView);
NumberOfWhacks.setText(String.valueOf(whacks));
如果(重击>最大重击){
maxwhacks=whacks;
TextView NumberOfMaxWhacks=findviewbyd(R.id.numberOfMaxWhacksView);
NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
}
pop();
}
}
});
slButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
如果(sl==1){
sl=0;
TextView ScrollLock=findViewById(R.id.sl);
scrollock.setText(“scr lk”);
whacks++;
TextView NumberOfWhacks=findView-ById(R.id.numberOfWhacksView);
NumberOfWhacks.setText(String.valueOf(whacks));
如果(重击>最大重击){
maxwhacks=whacks;
TextView NumberOfMaxWhacks=findviewbyd(R.id.numberOfMaxWhacksView);
NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
}
pop();
}
}
});
//从布局中捕获我们的按钮
开始=findViewById(R.id.startButton);
setOnClickListener(btnclicklListener);
cancel=findViewById(R.id.cancelButton);
cancel.setOnClickListener(btnclicklListener);
时间=findViewById(R.id.time);
//使用上述实现注册onClick侦听器
}
公共浮点数numAlea(){
返回r.nextFloat();
}
私有void start(){
如果(倒计时==null){
time.setText(“60”);
倒计时=新的倒计时(60*1000,1000){
@凌驾
公共void onTick(长毫秒未完成){
time.setText(“+millisuntillfinished/1000);
}
@凌驾
公共无效onFinish(){
cl=0;
nl=0;
sl=0;
whacks=0;
TextView CapsLock=findviewbyd(R.id.cl);
CapsLock.setText(“大写锁定”);
TextView NumLock=findviewbyd(R.id.nl);
NumLock.setText(“num lk”);
TextView ScrollLock=findViewById(R.id.sl);
scrollock.setText(“scr lk”);
倒计时=空;
setBestScore(maxwhacks);
public void setBestScore(int bestScore) {
SharedPreferences.Editor editor = getSharedPreferences("gamepanel", MODE_PRIVATE).edit();
editor.putInt("bestScore", maxwhacks);
editor.apply();
}
public int getBestScore() {
SharedPreferences prefs = getSharedPreferences("gamepanel", MODE_PRIVATE);
return prefs.getInt("bestScore", 0);
}
package com.example.android.whack_a_lock_022;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity<sharedPreferences, maxwhacks> extends AppCompatActivity {
public CountDownTimer countDownTimer = null;
int cl = 0;
int nl = 0;
int sl = 0;
int whacks = 0;
int maxwhacks = 0;
float random0to1;
Random r;
private Button start;
private Button cancel;
private TextView time;
TextView maxWhacksStored;
private SharedPreferences myPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
maxwhacks = getBestScore();
TextView maxWhacksStored = findViewById(R.id.numberOfMaxWhacksView);
maxWhacksStored.setText(maxwhacks);
r = new Random();
Button clButton = findViewById(R.id.cl);
Button nlButton = findViewById(R.id.nl);
Button slButton = findViewById(R.id.sl);
View.OnClickListener btnClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.startButton:
start();
break;
case R.id.cancelButton:
cancel();
break;
}
}
};
clButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (cl == 1) {
cl = 0;
TextView CapsLock = findViewById(R.id.cl);
CapsLock.setText("caps lock");
whacks++;
TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
NumberOfWhacks.setText(String.valueOf(whacks));
if (whacks > maxwhacks) {
maxwhacks = whacks;
TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
}
pop();
}
}
});
nlButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (nl == 1) {
nl = 0;
TextView NumLock = findViewById(R.id.nl);
NumLock.setText("num lk");
whacks++;
TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
NumberOfWhacks.setText(String.valueOf(whacks));
if (whacks > maxwhacks) {
maxwhacks = whacks;
TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
}
pop();
}
}
});
slButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sl == 1) {
sl = 0;
TextView ScrollLock = findViewById(R.id.sl);
ScrollLock.setText("scr lk");
whacks++;
TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
NumberOfWhacks.setText(String.valueOf(whacks));
if (whacks > maxwhacks) {
maxwhacks = whacks;
TextView NumberOfMaxWhacks = findViewById(R.id.numberOfMaxWhacksView);
NumberOfMaxWhacks.setText(String.valueOf(maxwhacks));
}
pop();
}
}
});
// Capture our button from layout
start = findViewById(R.id.startButton);
start.setOnClickListener(btnClickListener);
cancel = findViewById(R.id.cancelButton);
cancel.setOnClickListener(btnClickListener);
time = findViewById(R.id.time);
// Register the onClick listener with the implementation above
}
public float numAlea() {
return r.nextFloat();
}
private void start() {
if (countDownTimer == null) {
time.setText("60");
countDownTimer = new CountDownTimer(60 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
time.setText("" + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
cl = 0;
nl = 0;
sl = 0;
whacks = 0;
TextView CapsLock = findViewById(R.id.cl);
CapsLock.setText("caps lock");
TextView NumLock = findViewById(R.id.nl);
NumLock.setText("num lk");
TextView ScrollLock = findViewById(R.id.sl);
ScrollLock.setText("scr lk");
countDownTimer = null;
time.setText("Done !");
setBestScore(maxwhacks);
}
};
countDownTimer.start();
pop();
}
}
private void pop() {
// Escolhe e "POPa" o botão
// Gera número aleatório
random0to1 = numAlea();
if (random0to1 <= (1F / 3)) {
cl = 1;
Button clButton = findViewById(R.id.cl);
clButton.setText("CAPS LOCK");
} else if (random0to1 <= 2F / 3) {
nl = 1;
Button nlButton = findViewById(R.id.nl);
nlButton.setText("NUM LK");
} else if (random0to1 <= 1F) {
sl = 1;
Button slButton = findViewById(R.id.sl);
slButton.setText("SCR LK");
}
}
private void cancel() {
if (countDownTimer != null) {
countDownTimer.cancel();
countDownTimer = null;
}
//Põe botões a zeros
cl = 0;
Button clButton = findViewById(R.id.cl);
clButton.setText("caps lock");
nl = 0;
Button nlButton = findViewById(R.id.nl);
nlButton.setText("num lk");
sl = 0;
Button slButton = findViewById(R.id.sl);
slButton.setText("scr lk");
whacks = 0;
TextView NumberOfWhacks = findViewById(R.id.numberOfWhacksView);
NumberOfWhacks.setText("0");
}
public void setBestScore(int bestScore) {
SharedPreferences.Editor editor = getSharedPreferences("gamepanel", MODE_PRIVATE).edit();
editor.putInt("bestScore", maxwhacks);
editor.apply();
}
public int getBestScore() {
SharedPreferences prefs = getSharedPreferences("gamepanel", MODE_PRIVATE);
return prefs.getInt("bestScore", 0);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/whacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="WHACKS"
android:textSize="24sp"
android:textAlignment="center"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/maxWhacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="MAX WHACKS"
android:layout_weight="1"
android:textSize="24sp"
android:gravity="center_horizontal" />
</LinearLayout>
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="60"
android:textSize="24sp"
android:gravity="center_horizontal" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/numberOfWhacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0"
android:textAlignment="center"
android:textSize="24sp"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/numberOfMaxWhacksView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="0"
android:layout_weight="1"
android:textSize="24sp"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/cl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="caps lock"
android:layout_weight="1"
android:textAllCaps = "false"/>
<Button
android:id="@+id/nl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="num lk"
android:layout_weight="1"
android:textAllCaps = "false"/>
<Button
android:id="@+id/sl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="scr lk"
android:layout_weight="1"
android:textAllCaps = "false"/>
</LinearLayout>
<Button
android:id="@+id/startButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Start" />
<Button
android:id="@+id/cancelButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
maxWhacksStored.setText(previousMaxScore);
maxWhacksStored.setText(String.valueOf(previousMaxScore));