こんにちは、開発中のゲームで Google+ Games API を構築したいと考えています。しかし、ゲームがアクティビティで実行されないため、API にスコアを直接送信できないという問題があります。外部クラスの関数にスコアを送信したいので、次のコードを試しました。
private void updateGameOver(List<TouchEvent> touchEvents) {
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
Leaboarder lea = new Leaboarder();
lea.sendScore(Counter.getScore());
nullify();
game.setScreen(new MainMenuScreen(game));
return;
}
}
}
しかし、次のエラーが表示されます。
06-27 21:58:30.746: W/dalvikvm(5701): threadid=13: thread exiting with uncaught exception (group=0x40b88300)
06-27 21:58:30.756: E/AndroidRuntime(5701): FATAL EXCEPTION: Thread-46367
06-27 21:58:30.756: E/AndroidRuntime(5701): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-27 21:58:30.756: E/AndroidRuntime(5701): at android.os.Handler.<init>(Handler.java:121)
06-27 21:58:30.756: E/AndroidRuntime(5701): at android.app.Activity.<init>(Activity.java:749)
06-27 21:58:30.756: E/AndroidRuntime(5701): at de.niklaskellner.flydroid.gui.GoogleGame.Leaboarder.<init>(Leaboarder.java:20)
06-27 21:58:30.756: E/AndroidRuntime(5701): at de.niklaskellner.flydroid.GameScreen.updateGameOver(GameScreen.java:174)
06-27 21:58:30.756: E/AndroidRuntime(5701): at de.niklaskellner.flydroid.GameScreen.update(GameScreen.java:80)
06-27 21:58:30.756: E/AndroidRuntime(5701): at de.niklaskellner.framework.implementation.AndroidFastRenderView.run(AndroidFastRenderView.java:47)
06-27 21:58:30.756: E/AndroidRuntime(5701): at java.lang.Thread.run(Thread.java:856)
私のリーダークラス:
public class Leaboarder extends Activity implements GameHelper.GameHelperListener {
protected GameHelper mHelper;
protected int mRequestedClients = GameHelper.CLIENT_GAMES;
public static int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int score = getIntent().getIntExtra("score", 0);
sendScore(score);
}
public void sendScore(int score) {
mHelper = new GameHelper(Leaboarder.this);
mHelper.setup(Leaboarder.this, mRequestedClients);
GamesClient mGamesClient = mHelper.getGamesClient();
if (mHelper.isSignedIn()) {
mGamesClient.submitScore("", score);
}
else {
Toast.makeText(getBaseContext(), "Not Loggin - Score saved offline.", Toast.LENGTH_LONG).show();
SharedPreferences pref = getSharedPreferences("offlineScore", 0);
int scoreOLD = pref.getInt("topscore", 0);
if (score > scoreOLD){
pref.edit().putInt("topscore", score).apply();
}
}
}
}
私の質問は、どうすればその問題を回避できますか?