teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

新たな問題が発生

2021/09/30 02:33

投稿

aruko
aruko

スコア47

title CHANGED
File without changes
body CHANGED
@@ -21,4 +21,128 @@
21
21
  editor.apply();
22
22
 
23
23
  というように保存されていきます。
24
- よろしくお願いします。
24
+ よろしくお願いします。
25
+ 新たな問題が発生
26
+ ------------------------------------------------------------------------------------------
27
+ コメント投稿させてもらったように、新たな問題が発生しました。
28
+ MainActivity.javaの方ではなく、ResultActivity.javaの方で、
29
+ > SharedPreferences.Editor editor = sharedPreferences.edit();
30
+ editor.putInt("HIGH_SCORE", 0);
31
+ editor.apply();
32
+
33
+ を入れたら、ハイスコアは0にできたのですが、ResultActivity.javaでのゲームのスコアまで0になっていました。
34
+ ゲームのスコアは、PARAM_SCOREで引き継がれることになっています。
35
+ PARAM_SCOREについては何もさわっていません。(つもりです)
36
+ とりあえず次に、ハイスコアを0にするコードを消して下記の状態(つまりゲームのスコアもMainActivity.javaから引き継がれ、ハイスコアも正しい数値で出ていた状態)にしましたが、なぜかゲームのスコアが引き継がれず、0のままです。
37
+
38
+ 数回ゲームをしてみましたが、やっぱり0のままです。
39
+ これはどういうことでしょうか。
40
+ どうしたら元のように引き継がれるようになるのか、教えていただきたいです。
41
+ ```java
42
+ import androidx.appcompat.app.AppCompatActivity;
43
+
44
+ import android.app.Activity;
45
+ import android.content.Intent;
46
+ import android.content.SharedPreferences;
47
+ import android.os.Bundle;
48
+ import android.view.View;
49
+ import android.widget.Button;
50
+ import android.widget.ImageView;
51
+ import android.widget.TextView;
52
+
53
+ public class ResultActivity extends AppCompatActivity {
54
+ public static final String PARAM_SCORE = "SCORE";
55
+ public static final int RESULT_OWARU = Activity.RESULT_FIRST_USER;
56
+
57
+ public static final String SHARED_NAME = "GAME_DATA";
58
+ public static final String KEY_HIGHSCORE = "HIGH_SCORE";
59
+
60
+ @Override
61
+ protected void onCreate(Bundle savedInstanceState) {
62
+ super.onCreate(savedInstanceState);
63
+ setContentView(R.layout.activity_result);
64
+
65
+ int score;
66
+ int highscore;
67
+ TextView scoreLabel = findViewById(R.id.scoreLabel);
68
+ TextView highscoreLabel = findViewById(R.id.highscoreLabel);
69
+ TextView hukidasinaiyou=findViewById(R.id.hukidasinaiyou);
70
+ Button bt_hozon=findViewById(R.id.bt_hozon);
71
+ Button bt_mouitido=findViewById(R.id.bt_mouitido);
72
+ Button bt_owaru=findViewById(R.id.bt_owaru);
73
+ ImageView img_neko=findViewById(R.id.img_neko);
74
+ SharedPreferences sharedPreferences = getSharedPreferences("GAME_DATA", MODE_PRIVATE);
75
+ score = getIntent().getIntExtra("PARAM_SCORE", 0);
76
+ scoreLabel.setText(score + "てん");
77
+
78
+ hukidasinaiyou.setTextSize(18.0f);
79
+ highscore = sharedPreferences.getInt("HIGH_SCORE", 0);
80
+ highscoreLabel.setText( highscore+"てん");
81
+ if (score > highscore) {
82
+ hukidasinaiyou.setText("さいこうとうてんがでました。ほぞんしましょう!");
83
+ img_neko.setImageResource(R.drawable.neko_1ban);
84
+ bt_hozon.setVisibility(View.VISIBLE);
85
+ bt_mouitido.setVisibility(View.INVISIBLE);
86
+ bt_owaru.setVisibility(View.INVISIBLE);
87
+ }else{
88
+ bt_hozon.setVisibility(View.INVISIBLE);
89
+ bt_mouitido.setVisibility(View.VISIBLE);
90
+ bt_owaru.setVisibility(View.VISIBLE);
91
+ if(score==highscore) {
92
+ hukidasinaiyou.setText("さいこうとくてんと、どうてんですね!おしかったです。");
93
+ hukidasinaiyou.setTextSize(16.0f);
94
+ img_neko.setImageResource(R.drawable.neko_ponpon);
95
+ }else{
96
+ hukidasinaiyou.setText("つぎは、さいこうとくてんになるといいですね!");
97
+ img_neko.setImageResource(R.drawable.neko_annai
98
+ );
99
+ }
100
+ }
101
+ View.OnClickListener event = new View.OnClickListener() {
102
+ public void onClick(View view) {
103
+ if (view.getId() == R.id.bt_mouitido) {
104
+ setResult(Activity.RESULT_OK, null);
105
+ finish();
106
+ } else if (view.getId() == R.id.bt_hozon) {
107
+ highscoreLabel.setText(score + "てん");
108
+
109
+ SharedPreferences.Editor editor = sharedPreferences.edit();
110
+ editor.putInt("HIGH_SCORE", score);
111
+ editor.apply();
112
+
113
+ bt_hozon.setVisibility(View.INVISIBLE);
114
+ bt_owaru.setVisibility(View.VISIBLE);
115
+ bt_mouitido.setVisibility(View.VISIBLE);
116
+
117
+ } else if (view.getId() == R.id.bt_owaru) {
118
+ setResult(RESULT_OWARU, null);
119
+ finish();
120
+ }
121
+ }
122
+ };
123
+ bt_mouitido.setOnClickListener(event);
124
+ bt_hozon.setOnClickListener(event);
125
+ bt_owaru.setOnClickListener(event);
126
+ }
127
+ }
128
+ ```
129
+ ついでに、スコア引き継ぎに関するMainActivity部分は、これです。
130
+ ```java
131
+ private static final String TAG = "MainActivity";
132
+
133
+ private ActivityResultLauncher<Intent> startForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
134
+ switch (result.getResultCode()) {
135
+ case Activity.RESULT_OK:
136
+ syokisettei();
137
+ Log.d(TAG, "ActivityResultLaundher result=" + result);
138
+ break;
139
+ case ResultActivity.RESULT_OWARU:
140
+ Log.d(TAG, "ActivutyResultLaundher OWARU");
141
+ finish();
142
+ break;
143
+ }
144
+ });
145
+ ---------------------------------------------------------------------------------
146
+ startForResult.launch(new Intent(MainActivity.this, ResultActivity.class)
147
+ .putExtra(ResultActivity.PARAM_SCORE, score));
148
+ ```