首页 > 解决方案 > 在 Android(多个设备)上使用受信任的 Web 活动时出现 NullPointerException

问题描述

我正在使用TrustedWebActivities,我在三星 S8、S9 和 Pixel 2 设备上立即出现应用程序崩溃,并出现以下异常。一些研究表明,这可能发生在安装了不支持 TWA 的旧版 Chrome 并且后备方法失败的设备上。但我已经确认 Chrome 版本是 71(也适用于三星 S7、Pixel 3)。

java.lang.NullPointerException: 
  at android.support.customtabs.TrustedWebUtils.launchAsTrustedWebActivity (TrustedWebUtils.java:91)
  at android.support.customtabs.trusted.LauncherActivity$TwaCustomTabsServiceConnection.onCustomTabsServiceConnected (LauncherActivity.java:192)
  at android.support.customtabs.CustomTabsServiceConnection.onServiceConnected (CustomTabsServiceConnection.java:44)
  at android.app.LoadedApk$ServiceDispatcher.doConnected (LoadedApk.java:1656)
  at android.app.LoadedApk$ServiceDispatcher$RunConnection.run (LoadedApk.java:1685)
  at android.os.Handler.handleCallback (Handler.java:789)
  at android.os.Handler.dispatchMessage (Handler.java:98)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6938)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)

我相信这发生在sessionnull 的这一行上,它是一个 type 的对象CustomTabsSession。这是来源

  session.validateRelationship(CustomTabsService.RELATION_HANDLE_ALL_URLS, uri, null);

任何帮助表示赞赏。谢谢你。

标签: androidgoogle-chromeprogressive-web-appstrusted-web-activity

解决方案


此问题已修复为提交 https://github.com/GoogleChrome/custom-tabs-client/commit/51a4ff1d07d8507b934fc9d9af226a969b6c4674?diff=split#diff-39c75633d34184d98d2495b5686172dbL132-R136

您可以更新gradefile中的版本,它应该可以工作

implementation 'com.github.GoogleChrome.custom-tabs-client:customtabs:d08e93fce3fb3e1f22214ee2f23fddc4f4e92634'

正如您可以检查差异一样,验证功能已被删除。

session.validateRelationship(CustomTabsService.RELATION_HANDLE_ALL_URLS, uri, null);

然而,正如谷歌团队在自述文件中提到的那样,我们应该改用 Adnroid Browser Helper。

Please use the Android Browser Helper library instead. It contains the same functionality updated to work with AndroidX. https://github.com/GoogleChrome/android-browser-helper


推荐阅读