Android Appinvite 错误很遗憾 Google Play 服务已停止

Android Appinvite Error Unfortunately Google Play Services has stopped

我正在尝试在我的应用程序中实施应用程序邀请。这是我正在使用的 java 代码:

 Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
                    .build();
            startActivityForResult(intent, Constants.REQUEST_CODE_INVITE);

但是,当我点击执行这段代码的按钮时,会弹出一个对话框说 'Unfortunately, Google Play Services has stopped'. 我试过再次禁用、卸载和安装 google 播放服务,甚至重新启动 phone,但没有用。

android 工作室日志中没有显示异常,所以我无法找出问题所在。

请帮忙。

这里有一些问题,我还请你验证一下值。

  1. getString 需要资源。假设您在 activity 中调用它,您需要调用 getResources().getString(R.......).你可能会得到 null 返回这将导致异常。
  2. 虽然只有1个必填参数,但您应该多填几个以确保您有一个合理的邀请,特别是setMessage()。此外,如果您知道帐户,则可以使用 setAccount() ,否则会提示。

我还会验证输入值。例如,记录您为 R.string.invitation_title 获得的字符串值,以确保在调用构建器之前已正确设置它。

我犯了一个导致错误的愚蠢错误。

我在发送意图之前没有初始化 GoogleApiClient。不知道我是怎么错过的。 这是我遗漏的代码:

GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(AppInvite.API)
            .enableAutoManage(this, this)
            .build();

希望它能帮助到此页面的未来访问者。