質問編集履歴

1

新たな問題が発生

2021/09/30 02:33

投稿

aruko
aruko

スコア47

test CHANGED
File without changes
test CHANGED
@@ -45,3 +45,251 @@
45
45
  というように保存されていきます。
46
46
 
47
47
  よろしくお願いします。
48
+
49
+ 新たな問題が発生
50
+
51
+ ------------------------------------------------------------------------------------------
52
+
53
+ コメント投稿させてもらったように、新たな問題が発生しました。
54
+
55
+ MainActivity.javaの方ではなく、ResultActivity.javaの方で、
56
+
57
+ > SharedPreferences.Editor editor = sharedPreferences.edit();
58
+
59
+ editor.putInt("HIGH_SCORE", 0);
60
+
61
+ editor.apply();
62
+
63
+
64
+
65
+ を入れたら、ハイスコアは0にできたのですが、ResultActivity.javaでのゲームのスコアまで0になっていました。
66
+
67
+ ゲームのスコアは、PARAM_SCOREで引き継がれることになっています。
68
+
69
+ PARAM_SCOREについては何もさわっていません。(つもりです)
70
+
71
+ とりあえず次に、ハイスコアを0にするコードを消して下記の状態(つまりゲームのスコアもMainActivity.javaから引き継がれ、ハイスコアも正しい数値で出ていた状態)にしましたが、なぜかゲームのスコアが引き継がれず、0のままです。
72
+
73
+
74
+
75
+ 数回ゲームをしてみましたが、やっぱり0のままです。
76
+
77
+ これはどういうことでしょうか。
78
+
79
+ どうしたら元のように引き継がれるようになるのか、教えていただきたいです。
80
+
81
+ ```java
82
+
83
+ import androidx.appcompat.app.AppCompatActivity;
84
+
85
+
86
+
87
+ import android.app.Activity;
88
+
89
+ import android.content.Intent;
90
+
91
+ import android.content.SharedPreferences;
92
+
93
+ import android.os.Bundle;
94
+
95
+ import android.view.View;
96
+
97
+ import android.widget.Button;
98
+
99
+ import android.widget.ImageView;
100
+
101
+ import android.widget.TextView;
102
+
103
+
104
+
105
+ public class ResultActivity extends AppCompatActivity {
106
+
107
+ public static final String PARAM_SCORE = "SCORE";
108
+
109
+ public static final int RESULT_OWARU = Activity.RESULT_FIRST_USER;
110
+
111
+
112
+
113
+ public static final String SHARED_NAME = "GAME_DATA";
114
+
115
+ public static final String KEY_HIGHSCORE = "HIGH_SCORE";
116
+
117
+
118
+
119
+ @Override
120
+
121
+ protected void onCreate(Bundle savedInstanceState) {
122
+
123
+ super.onCreate(savedInstanceState);
124
+
125
+ setContentView(R.layout.activity_result);
126
+
127
+
128
+
129
+ int score;
130
+
131
+ int highscore;
132
+
133
+ TextView scoreLabel = findViewById(R.id.scoreLabel);
134
+
135
+ TextView highscoreLabel = findViewById(R.id.highscoreLabel);
136
+
137
+ TextView hukidasinaiyou=findViewById(R.id.hukidasinaiyou);
138
+
139
+ Button bt_hozon=findViewById(R.id.bt_hozon);
140
+
141
+ Button bt_mouitido=findViewById(R.id.bt_mouitido);
142
+
143
+ Button bt_owaru=findViewById(R.id.bt_owaru);
144
+
145
+ ImageView img_neko=findViewById(R.id.img_neko);
146
+
147
+ SharedPreferences sharedPreferences = getSharedPreferences("GAME_DATA", MODE_PRIVATE);
148
+
149
+ score = getIntent().getIntExtra("PARAM_SCORE", 0);
150
+
151
+ scoreLabel.setText(score + "てん");
152
+
153
+
154
+
155
+ hukidasinaiyou.setTextSize(18.0f);
156
+
157
+ highscore = sharedPreferences.getInt("HIGH_SCORE", 0);
158
+
159
+ highscoreLabel.setText( highscore+"てん");
160
+
161
+ if (score > highscore) {
162
+
163
+ hukidasinaiyou.setText("さいこうとうてんがでました。ほぞんしましょう!");
164
+
165
+ img_neko.setImageResource(R.drawable.neko_1ban);
166
+
167
+ bt_hozon.setVisibility(View.VISIBLE);
168
+
169
+ bt_mouitido.setVisibility(View.INVISIBLE);
170
+
171
+ bt_owaru.setVisibility(View.INVISIBLE);
172
+
173
+ }else{
174
+
175
+ bt_hozon.setVisibility(View.INVISIBLE);
176
+
177
+ bt_mouitido.setVisibility(View.VISIBLE);
178
+
179
+ bt_owaru.setVisibility(View.VISIBLE);
180
+
181
+ if(score==highscore) {
182
+
183
+ hukidasinaiyou.setText("さいこうとくてんと、どうてんですね!おしかったです。");
184
+
185
+ hukidasinaiyou.setTextSize(16.0f);
186
+
187
+ img_neko.setImageResource(R.drawable.neko_ponpon);
188
+
189
+ }else{
190
+
191
+ hukidasinaiyou.setText("つぎは、さいこうとくてんになるといいですね!");
192
+
193
+ img_neko.setImageResource(R.drawable.neko_annai
194
+
195
+ );
196
+
197
+ }
198
+
199
+ }
200
+
201
+ View.OnClickListener event = new View.OnClickListener() {
202
+
203
+ public void onClick(View view) {
204
+
205
+ if (view.getId() == R.id.bt_mouitido) {
206
+
207
+ setResult(Activity.RESULT_OK, null);
208
+
209
+ finish();
210
+
211
+ } else if (view.getId() == R.id.bt_hozon) {
212
+
213
+ highscoreLabel.setText(score + "てん");
214
+
215
+
216
+
217
+ SharedPreferences.Editor editor = sharedPreferences.edit();
218
+
219
+ editor.putInt("HIGH_SCORE", score);
220
+
221
+ editor.apply();
222
+
223
+
224
+
225
+ bt_hozon.setVisibility(View.INVISIBLE);
226
+
227
+ bt_owaru.setVisibility(View.VISIBLE);
228
+
229
+ bt_mouitido.setVisibility(View.VISIBLE);
230
+
231
+
232
+
233
+ } else if (view.getId() == R.id.bt_owaru) {
234
+
235
+ setResult(RESULT_OWARU, null);
236
+
237
+ finish();
238
+
239
+ }
240
+
241
+ }
242
+
243
+ };
244
+
245
+ bt_mouitido.setOnClickListener(event);
246
+
247
+ bt_hozon.setOnClickListener(event);
248
+
249
+ bt_owaru.setOnClickListener(event);
250
+
251
+ }
252
+
253
+ }
254
+
255
+ ```
256
+
257
+ ついでに、スコア引き継ぎに関するMainActivity部分は、これです。
258
+
259
+ ```java
260
+
261
+ private static final String TAG = "MainActivity";
262
+
263
+
264
+
265
+ private ActivityResultLauncher<Intent> startForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
266
+
267
+ switch (result.getResultCode()) {
268
+
269
+ case Activity.RESULT_OK:
270
+
271
+ syokisettei();
272
+
273
+ Log.d(TAG, "ActivityResultLaundher result=" + result);
274
+
275
+ break;
276
+
277
+ case ResultActivity.RESULT_OWARU:
278
+
279
+ Log.d(TAG, "ActivutyResultLaundher OWARU");
280
+
281
+ finish();
282
+
283
+ break;
284
+
285
+ }
286
+
287
+ });
288
+
289
+ ---------------------------------------------------------------------------------
290
+
291
+ startForResult.launch(new Intent(MainActivity.this, ResultActivity.class)
292
+
293
+ .putExtra(ResultActivity.PARAM_SCORE, score));
294
+
295
+ ```