androidstudioでスマホアプリ作りの途中です。
ゲームの終了時に、ダイアログを表示させ、「保存する」のボタンで結果画面に遷移させたいですが、コード上でエラーになります。
なので、とりあえずMainActivity.javaに、
java
1Intent intent = new Intent(getApplicationContext(), ResultActivity.class); 2intent.putExtra("SCORE", score); 3startActivity(intent);
を入れてみたのですが、
実行し、この部分にくると画面遷移しないでいきなり終了してしまいます。
やりたいのは、ダイアログから画面遷移ですが、まず、画面遷移のしかたでどこが悪いのか教えていただきたいです。
ResultActivity.javaは、ネットからコピーさせてもらったものですが、こうです。activity_result.xmlはできています。
java
1import androidx.appcompat.app.AppCompatActivity; 2 3import android.content.Intent; 4import android.content.SharedPreferences; 5import android.os.Bundle; 6import android.view.View; 7import android.widget.TextView; 8 9public class ResultActivity extends AppCompatActivity { 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_result); 15 16 TextView scoreLabel = findViewById(R.id.scoreLabel); 17 TextView highScoreLabel = findViewById(R.id.highScoreLabel); 18 19 int score = getIntent().getIntExtra("SCORE", 0); 20 scoreLabel.setText(score + ""); 21 22 SharedPreferences sharedPreferences = getSharedPreferences("GAME_DATA", MODE_PRIVATE); 23 int highScore = sharedPreferences.getInt("HIGH_SCORE", 0); 24 25 if (score > highScore) { 26 highScoreLabel.setText("High Score : " + score); 27 28 SharedPreferences.Editor editor = sharedPreferences.edit(); 29 editor.putInt("HIGH_SCORE", score); 30 editor.apply(); 31 32 } else { 33 highScoreLabel.setText("High Score : " + highScore); 34 } 35 } 36 public void tryAgain(View view) { 37 startActivity(new Intent(getApplicationContext(), MainActivity.class)); 38 } 39}
回答1件
あなたの回答
tips
プレビュー