Crocme
Crocme

Reputation: 1

App crash when is submitting score leaderboard Google Play

I have some problems with my android application.

I use google play games services with leaderboards. I import all the libraries, I do all that s needed...

In my main activity I follow the Google developper documentation here and I use:

mClient.connect();

for the connection and that works.

At the end of the game, I try to submit the score. I saw on forums that I can put mClient in static to use it in the other activity. So I call in onCreate :

Games.Leaderboards.submitScore(MainActivity.mClient, getString(R.string.class_1), myscore);

But the app crash when score is submitting. I have these error:

12-14 15:37:24.856: E/AndroidRuntime(19613): FATAL EXCEPTION: main
12-14 15:37:24.856: E/AndroidRuntime(19613): java.lang.RuntimeException: Unable to resume activity {com.test.app/com.test.app.Result}: java.lang.IllegalStateException: GoogleApiClient must be connected.
12-14 15:37:24.856: E/AndroidRuntime(19613):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2141)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2156)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1680)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at android.app.ActivityThread.access$1500(ActivityThread.java:121)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at android.os.Looper.loop(Looper.java:130)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at android.app.ActivityThread.main(ActivityThread.java:3701)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at java.lang.reflect.Method.invokeNative(Native Method)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at java.lang.reflect.Method.invoke(Method.java:507)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at dalvik.system.NativeStart.main(Native Method)
12-14 15:37:24.856: E/AndroidRuntime(19613): Caused by: java.lang.IllegalStateException: GoogleApiClient must be connected.
12-14 15:37:24.856: E/AndroidRuntime(19613):    at com.google.android.gms.common.internal.o.a(Unknown Source)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at com.google.android.gms.games.Games.c(Unknown Source)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at com.google.android.gms.games.internal.api.LeaderboardsImpl.submitScore(Unknown Source)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at com.google.android.gms.games.internal.api.LeaderboardsImpl.submitScore(Unknown Source)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at com.test.app.Result.onResume(Result.java:129)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at android.app.Activity.performResume(Activity.java:3832)
12-14 15:37:24.856: E/AndroidRuntime(19613):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2131)
12-14 15:37:24.856: E/AndroidRuntime(19613):    ... 12 more

At the beginning, it write "GoogleApiClient must be connected" but "mClient" is connected. Therefor I try also to submit score in the MainActivity without mClient in static but the app crash also.

Upvotes: 0

Views: 965

Answers (1)

Clayton Wilkinson
Clayton Wilkinson

Reputation: 4572

The best practice for managing the Google API client is for each activity to maintain its own client state. See this question for more detailed discussion: Access google plus client from multiple activities.

The user experience will be signing in on the "main activity", and then in the activity at the end of the game, check for client.isConnected() (and call connect if not connected) then once connected submit the score. This way, you will make sure you have a valid client before calling the API.

Upvotes: 1

Related Questions