我试着在应用程序开始时检查邀请函,但是每次代码运行时。包返回null。我在谷歌的开发者网站上看到
返回的任务不会为空,但如果已经收到或没有找到相关数据,则从任务返回的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);
}
}
}
});
}发布于 2018-05-10 12:30:46
不知道这是否对您有帮助,但我使用了来自GoogleDeveloper站点的相同代码来检查邀请,我的应用程序每次在没有邀请的情况下启动时都会崩溃。
I get: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法的android.os.Parcelable android.os.Parcelable。
当我收到邀请通知并通过statusbar启动应用程序时,它不会崩溃,并能像预期的那样工作。因此,这是可以的,但我无法启动应用程序不崩溃,只要没有邀请。
我不想在没有邀请的情况下崩溃的解决办法是:
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
}
}
}
}https://stackoverflow.com/questions/49168296
复制相似问题