【问题标题】:Google Multiplayer RealTime invitations谷歌多人实时邀请
【发布时间】:2018-03-08 08:10:41
【问题描述】:

我试图在应用程序启动时检查邀请,但每次代码运行时。捆绑包返回 null。我在谷歌的开发者网站上看到了

返回的Task不会为null,但是如果Task返回的Bundle已经收到或者没有找到相关数据,则可能为null。

这是我用来检查邀请的代码:

private void checkForInvitation() {
    Games.getGamesClient(this, GoogleSignIn.getLastSignedInAccount(this)).getActivationHint()
            .addOnSuccessListener(new OnSuccessListener<Bundle>() {
                @Override
                public void onSuccess(Bundle bundle) {
                    System.out.println(bundle);
                    if(bundle != null) {
                        System.out.println("three line");
                        Invitation invitation = bundle.getParcelable(Multiplayer.EXTRA_INVITATION);
                        if (invitation != null) {
                            System.out.println("fourth line");
                            notificationText = String.valueOf(bundle.size());
                            RoomConfig.Builder builder = RoomConfig.builder(mRoomUpdateCallback)
                                    .setInvitationIdToAccept(invitation.getInvitationId());
                            mJoinedRoomConfig = builder.build();
                            // prevent screen from sleeping during handshake
                            getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                        }
                    }
                }
            });

}

【问题讨论】:

  • 您是否尝试过在 Google Console 中为开发者打开启用多人游戏按钮?

标签: android libgdx google-play-services google-play-games multiplayer


【解决方案1】:

不知道这是否对您有帮助,但我使用来自 GoogleDeveloper 网站的相同代码来检查邀请,并且我的应用每次在没有邀请的情况下启动时都会崩溃。

我得到:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)'。

当我收到邀请通知并通过状态栏启动应用程序时,它不会崩溃并且按预期工作。所以这没关系,但只要没有邀请,我就无法在没有崩溃的情况下启动应用程序。

我在没有邀请的情况下不崩溃的解决方案是:

new OnSuccessListener<Bundle>() {
    @Override
    public void onSuccess(Bundle bundle) {
        if (bundle != null){
            Invitation invitation = bundle.getParcelable(Multiplayer.EXTRA_INVITATION);
            if (invitation != null) {
                //do what you want to do when invitation reveived
            } 
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    相关资源
    最近更新 更多