1

I'm trying to add a leaderboard in a game using Google Play Game Services.

Classes in which I display leaderboard and submit score extend Screen from Framework, so I created that Screen extends BaseGameActivity, which is required to use Google Play Game Services.

I get this error when I start app:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
            at android.os.Handler.<init>(Handler.java:197)
            at android.os.Handler.<init>(Handler.java:111)
            at android.app.Activity.<init>(Activity.java:784)
            at android.support.v4.app.FragmentActivity.<init>(FragmentActivity.java:76)
            at com.x.x.BaseGameActivity.<init>(BaseGameActivity.java:65)
            at com.x.framework.Screen.<init>(Screen.java:8)
            at com.x.x.LoadingScreen.<init>(LoadingScreen.java:11)
            at com.x.x.SplashLoadingScreen.update(SplashLoadingScreen.java:19)
            at com.x.framework.implementation.AndroidFastRenderView.run(AndroidFastRenderView.java:47)
            at java.lang.Thread.run(Thread.java:841)

Part of code where error is pointing:

BaseGameActivity.java

...protected BaseGameActivity() {
    super();
}...

Screen.java

...public Screen(Game game) {
        this.game = game;
    }...

LoadingScreen.java

...public LoadingScreen(Game game) {

        super(game);
    }...

SplashLoadingScreen.java

...game.setScreen(new LoadingScreen(game));...

AndroidFastRenderView.java

 ...game.getCurrentScreen().update(deltaTime);...

Does anyone know how to fix this?

laalto
  • 150,114
  • 66
  • 286
  • 303
Ren S
  • 59
  • 2
  • 8

2 Answers2

1

Based on the stacktrace, it looks like you are trying to touch views and create Activity in a background thread. You should not. Try to do it on main thread and you will be fine.

huy.nguyen
  • 454
  • 2
  • 9
0
  1. Don't attempt to update user interface from a background thread. Use e.g. runOnUiThread() to post a Runnable to a handler running on the main UI thread.

  2. Don't attempt to instantiate activity classes such as LoadingScreen yourself with new. Use only Intent to indirectly instantiate activities or change your design so that the class you're instantiating is not an Activity.

laalto
  • 150,114
  • 66
  • 286
  • 303