我正在尝试检查应用启动时的邀请,但每次代码都会运行。该包返回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);
}
}
}
});
}
答案 0 :(得分:0)
不知道这是否对您有所帮助,但我使用GoogleDeveloper网站上的相同代码检查邀请,每次启动时我的应用都会崩溃没有邀请。
我得到:java.lang.NullPointerException:尝试调用虚方法&#39; android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)&#39;在null对象引用上。
当我收到邀请通知并通过状态栏启动应用时,它不会崩溃并按预期工作。所以这没关系,但只要没有邀请,我就无法在没有崩溃的情况下启动应用程序。
我没有邀请就崩溃的解决方案是:
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
}
}
}
}