前提・実現したいこと・問題
いつもお世話になっております。
androidstudioで、簡単なゲームアプリを作っています。
画面の流れとしては、
スタート画面(activity_screenstart.xml)
↓
ゲーム画面(MainView.java)
↓
リトライ画面(activity_screenretry.xml)
となっています。
そのほかに、ハイスコアを管理するPlayLog.javaがあります。
スタート画面にハイスコアの表示をしたいので、
MainActivityのsetScreenStart()で、
setTextを使って表示されるようにしているつもりなのですが、
うまく使えていないようで、画面上にハイスコアがまったく表示されません。
どこが間違っているのでしょうか。。
よろしくお願いいたします。
###エラーメッセージ
07-08 14:40:06.648 19997-19997/? E/Zygote: isWhitelistProcess - Process is Whitelisted
07-08 14:40:06.648 19997-19997/? E/libpersona: scanKnoxPersonas
Couldn't open the File - /data/system/users/0/personalist.xml - No such file or directory
該当のソースコード
java
1package com.hachiya.candyzombie; 2 3import android.app.Activity; 4import android.content.pm.ActivityInfo; 5import android.os.Bundle; 6import android.os.Handler; 7import android.view.SurfaceView; 8import android.view.View; 9import android.widget.ImageButton; 10 11public class MainActivity extends Activity { 12 13 private SurfaceView view; 14 15 private Handler handlerA = new Handler(); 16 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 21 22 PlayLog.init(this); 23 24 setScreenStart(); 25 } 26 27 //スタート画面 28 public void setScreenStart() { 29 setContentView(R.layout.activity_screenstart); 30 31 int score = PlayLog.bestScore(); 32 TextView textView = findViewById(R.id.highScore); 33 textView.setText(String.valueOf(score)); 34 35 ImageButton startButton = findViewById(R.id.start_button); 36 37 startButton.setOnClickListener(new View.OnClickListener() { 38 @Override 39 public void onClick(View v) { 40 41 setScreenPlay(); 42 } 43 }); 44 } 45 46 //リトライ画面 47 public void setScreenRetry() { 48 setContentView(R.layout.activity_screenretry); 49 50 ImageButton retryButton = findViewById(R.id.retry_button); 51 52 retryButton.setOnClickListener(new View.OnClickListener() { 53 @Override 54 public void onClick(View v) { 55 setScreenStart(); 56 } 57 }); 58 } 59 60 //プレイ画面 61 public void setScreenPlay() { 62 63 view = new MainView(this); 64 setContentView(view); 65 66 // 30秒後に1度だけ処理を実行 67 handlerA.postDelayed(new Runnable() { 68 public void run() { 69 setScreenRetry(); 70 }, 30 * 1000); 71 72}
xml
1<!--activity_screenstart--> 2 3<?xml version="1.0" encoding="utf-8"?> 4 5<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 6 xmlns:tools="http://schemas.android.com/tools" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 tools:context=".MainActivity"> 10 11 <ImageView 12 android:id="@+id/startImg" 13 android:layout_width="match_parent" 14 android:layout_height="match_parent" 15 android:layout_centerHorizontal="true" 16 android:layout_centerVertical="true" 17 android:scaleType="centerCrop" 18 android:src="@drawable/bgimg1" /> 19 20 <ImageButton 21 android:id="@+id/start_button" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:layout_centerInParent="true" 25 android:background="@drawable/start" /> 26 27 <ImageView 28 android:id="@+id/player" 29 android:layout_width="200dp" 30 android:layout_height="150dp" 31 android:layout_alignTop="@+id/start_button" 32 android:layout_toLeftOf="@+id/start_button" 33 android:src="@drawable/player" /> 34 35 <ImageView 36 android:layout_width="300dp" 37 android:layout_height="200dp" 38 android:layout_toLeftOf="@+id/start_button" 39 android:layout_above="@+id/player" 40 android:src="@drawable/startwords" /> 41 42 <TextView 43 android:id="@+id/Title" 44 android:layout_width="wrap_content" 45 android:layout_height="wrap_content" 46 android:text="@string/title" 47 android:textSize="30sp" 48 android:textColor="#FFFFFF" 49 android:layout_centerHorizontal="true" 50 android:layout_above="@id/start_button" 51 android:layout_marginBottom="20dp"/> 52 53 <TextView 54 android:id="@+id/highScore" 55 android:layout_width="wrap_content" 56 android:layout_height="wrap_content" 57 /> 58 59</RelativeLayout>
java
1package com.hachiya.candyzombie; 2 3import android.content.Context; 4import android.content.res.Resources; 5import android.graphics.Bitmap; 6import android.graphics.BitmapFactory; 7import android.graphics.Canvas; 8import android.graphics.Color; 9import android.graphics.Paint; 10import android.media.MediaPlayer; 11import android.view.MotionEvent; 12import android.view.SurfaceHolder; 13import android.view.SurfaceView; 14 15import java.util.Random; 16 17// SurfaceViewクラスを継承したクラスを作成 18public class MainView extends SurfaceView implements SurfaceHolder.Callback, Runnable { 19 20 private int score = 0; 21 22 SurfaceHolder surfaceHolder; 23 Thread thread; 24 25 //コンストラクタ 26 public MainView(Context context) { 27 super(context); 28 29 surfaceHolder = getHolder(); 30 surfaceHolder.addCallback(this); 31 32 PlayLog.init(context); 33 34 } 35 36 @Override 37 public void surfaceCreated(SurfaceHolder holder) { 38 thread = new Thread(this); 39 thread.start(); 40 } 41 42 @Override 43 public void surfaceDestroyed(SurfaceHolder holder) { 44 thread = null; 45 } 46 47 @Override 48 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 49 50 } 51 52 @Override 53 public void run() { 54 55 Canvas canvas = null; 56 57 while (thread != null) { 58 59 try { 60 61 PlayLog.logScore(score); 62 63 //描画開始 64 canvas = surfaceHolder.lockCanvas(); 65 66 //省略(ゲーム操作に応じて、scoreが更新されるようになっています) 67 68 //描画終了 69 surfaceHolder.unlockCanvasAndPost(canvas); 70 } 71 catch (Exception e) { } 72 } 73 } 74 75}
java
1package com.hachiya.candyzombie; 2 3import android.content.Context; 4import android.content.SharedPreferences; 5 6public final class PlayLog { 7 8 private static final String FILENAME_PLAYLOG = "playlog"; 9 private static Context context; 10 11 private static int lastScore = 0; 12 private static int highScore = 0; 13 14 public static void init(final Context context) { 15 16 PlayLog.context = context; 17 load(); 18 } 19 20 public static void logScore(final int score) { 21 22 lastScore = score; 23 24 if (highScore < lastScore) { 25 highScore = score; 26 save(); 27 } 28 } 29 30 public static int lastScore() { 31 return lastScore; 32 } 33 34 public static int bestScore() { 35 return highScore; 36 } 37 38 public static void save() { 39 SharedPreferences preference = context.getSharedPreferences(FILENAME_PLAYLOG, Context.MODE_PRIVATE); 40 SharedPreferences.Editor editor = preference.edit(); 41 editor.putInt("hScore", highScore); 42 editor.commit(); 43 } 44 45 public static void load() { 46 SharedPreferences preference = context.getSharedPreferences(FILENAME_PLAYLOG, Context.MODE_PRIVATE); 47 highScore = preference.getInt("hScore", 0); 48 } 49}
補足情報
回答1件
あなたの回答
tips
プレビュー