我正在尝试使用新的GoogleAPIClient并进行连接,但是每当我在onConnected()的回调中调用Games.Players.getCurrentPlayer时,我都会得到这样的结果:
04-28 21:26:40.931: E/AndroidRuntime(386): java.lang.UnsupportedOperationException: Cannot add data to empty builder
04-28 21:26:40.931: E/AndroidRuntime(386): at bgs.a(SourceFile:915)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.google.android.gms.internal.gb$a$a.fI(Unknown Source)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.google.android.gms.internal.fx.fp(Unknown Source)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.google.android.gms.internal.gm.getCurrentPlayer(Unknown Source)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.djs.android.tictactoe.startpage.ActivityStartPage.onConnected(ActivityStartPage.java:540)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.google.android.gms.internal.ei.b(Unknown Source)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.google.android.gms.common.api.b.dy(Unknown Source)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.google.android.gms.common.api.b.d(Unknown Source)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.google.android.gms.common.api.b$2.onConnected(Unknown Source)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.google.android.gms.internal.ei.b(Unknown Source)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.google.android.gms.internal.ei.bo(Unknown Source)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.google.android.gms.internal.eh$h.b(Unknown Source)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.google.android.gms.internal.eh$h.a(Unknown Source)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.google.android.gms.internal.eh$b.ec(Unknown Source)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.google.android.gms.internal.eh$a.handleMessage(Unknown Source)
04-28 21:26:40.931: E/AndroidRuntime(386): at android.os.Handler.dispatchMessage(Handler.java:102)
04-28 21:26:40.931: E/AndroidRuntime(386): at android.os.Looper.loop(Looper.java:136)
04-28 21:26:40.931: E/AndroidRuntime(386): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-28 21:26:40.931: E/AndroidRuntime(386): at java.lang.reflect.Method.invoke(Native Method)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-28 21:26:40.931: E/AndroidRuntime(386): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)这里是创建GoogleAPIClient的代码。
mGoogleClientAPI = new GoogleApiClient.Builder(this).addApi(Games.API)
.addScope(Games.SCOPE_GAMES).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();下面是OnConnected方法的代码。
public void onConnected(Bundle bundle)
{
if (mConnectionProgressDialog.isShowing())
{
mConnectionProgressDialog.dismiss();
}
String playerid = Games.Players.getCurrentPlayerId(mGoogleClientAPI);
Player plr = Games.Players.getCurrentPlayer(mGoogleClientAPI);
MySigninButton.PlayerSignedIn(plr);
}我可以很好地访问排行榜和成果,我也可以毫无问题地调用Games.Players.getCurrentPlayerId()。
任何建议都将不胜感激,谢谢大家。
发布于 2014-07-14 11:29:04
我设置它的方法是在onCreate()方法的Activity类中构建Google API客户端。
this.mGoogleClient = new GoogleApiClient.Builder(this, this, this)
.addApi(Games.API)
.addScope(Games.SCOPE_GAMES)
.setViewForPopups(this.findViewById(R.id.sign_in_button))
.setGravityForPopups(Gravity.TOP | Gravity.CENTER_HORIZONTAL)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();isShowing()方法应该在onConnected()中工作,这无论如何都会抛出一个NPE。
如果不在Google Play控制台上启用多人游戏,你也无法连接或进入排行榜,所以看起来没问题。
有趣的是,你可以访问播放器ID,这导致我得出结论,你应该检查一下,确保你试图创建的播放器对象确实是应用编程接口的一部分(也许你正试图将它设置为你自己版本的播放器对象),并再次确保在调用onConnected()之前,谷歌客户端已经构建好并就位了。
我没有一个可靠的答案,但我已经用我自己的应用程序经历了这个过程,所以我觉得我们可以达成一个解决方案。
https://stackoverflow.com/questions/23355539
复制相似问题