Hi I whant to build the Google+ Games API in my Game that I am developing. But I have the problem that I can't send scores directly to the API because the Game dosen't run in an Activity. So I whant to send the score to a function in an external class so I tried this code:
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;
}
}
}
But I get the following error:
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)
My Leaboarder class:
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();
}
}
}
}
My Question is how can I work around that problem?
Just call Looper.prepare();
as second statement in your onCreate
method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.