Java Android应用程序启动崩溃-customtabs.CustomTabsession.validateRelationship
我最近遵循了谷歌的指导方针,我的应用程序在我的设备和一些精选的模拟器上运行良好 然而,在某些设备和模拟器上,如谷歌像素2,我在启动时会立即崩溃 logcat输出:Java Android应用程序启动崩溃-customtabs.CustomTabsession.validateRelationship,java,android,Java,Android,我最近遵循了谷歌的指导方针,我的应用程序在我的设备和一些精选的模拟器上运行良好 然而,在某些设备和模拟器上,如谷歌像素2,我在启动时会立即崩溃 logcat输出: E/AndroidRuntime: FATAL EXCEPTION: main Process: com.ocdev.yu_gi_ohdatabase, PID: 5020 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ocdev.yu_gi_ohdatabase, PID: 5020
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.customtabs.CustomTabsSession.validateRelationship(int, android.net.Uri, android.os.Bundle)' on a null object reference
at android.support.customtabs.TrustedWebUtils.launchAsTrustedWebActivity(TrustedWebUtils.java:134)
at android.support.customtabs.trusted.LauncherActivity$TwaCustomTabsServiceConnection.onCustomTabsServiceConnected(LauncherActivity.java:199)
at android.support.customtabs.CustomTabsServiceConnection.onServiceConnected(CustomTabsServiceConnection.java:44)
at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1634)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1663)
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:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ocdev.yu_gi_ohdatabase">
<application
android:allowBackup="true"
android:icon="@mipmap/ygo_fore"
android:label="@string/app_name"
android:roundIcon="@mipmap/ygo_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="asset_statements"
android:resource="@string/asset_statements" />
<activity
android:name="android.support.customtabs.trusted.LauncherActivity">
<!-- Edit android:value to change the url opened by the TWA -->
<meta-data
android:name="android.support.customtabs.trusted.DEFAULT_URL"
android:value="https://db.ygoprodeck.com" />
<!-- This intent-filter adds the TWA to the Android Launcher -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!--
This intent-filter allows the TWA to handle Intents to open
airhorner.com.
-->
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE"/>
<!-- Edit android:host to handle links to the target URL-->
<data
android:scheme="https"
android:host="db.ygoprodeck.com"/>
</intent-filter>
</activity>
</application>
</manifest>
Project build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.ocdev.yu_gi_ohdatabase"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.GoogleChrome.custom-tabs-client:customtabs:3a71a75c9f'
implementation 'com.android.support:support-media-compat:28.0.0'
implementation 'com.android.support:animated-vector-drawable:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
我还通过Android模拟器测试了我发布的apk,我收到了完全相同的问题。
我也在一些我测试的物理设备上收到了问题,比如三星Galaxy S7 Edge,但奇怪的是,我测试了不同的Galaxy S7 Edge,它工作正常。从我目前为止的测试来看,我猜它在我测试过的50%的设备上都能工作。
我不是继承安卓系统的开发者,所以我很难找到这个问题的原因,以及为什么它在一些设备上运行良好,而在其他设备上运行不好 我也有同样的问题 我注意到,如果我将chrome更新到目前的最新版本,TWA将非常有效。 令人恼火的是,用户在更新chrome时没有错误信息
这使得TWA目前无法使用 我的TWA也有这个问题,我发现如果浏览器默认设置为chrome或edge,那么它就可以工作,但对三星浏览器和firefox不起作用我在chrome论坛上发布了这个问题,并被重定向到列出的临时修复程序 简言之,他们意识到了这个问题,并希望很快找到一个永久性的解决方案,所以暂时的解决方案应该不会需要太久 答案如下: 目前,您可以将最后一个参数更改为 CustomTabsClientgetPackageName为true。此参数为 “ignoreDefault”和您可能猜到的将使包 选择忽略用户的默认设置。理想情况下,虽然这不会发生 需要很长时间
新版本可能不再出现此问题。但是,如果有人仍然使用旧版本,则可以帮助更新到新版本。 在新版本中,已删除验证功能,如下所示: 允许从TWA客户端发送多个受信任的来源 当前,如果用户能够导航或被重定向到 TWA中的另一个可信来源,是确保 通过调用 客户端上的CustomTabsessionVerifyRelationship。除了 有点笨拙的api,这会延迟Chrome的发布,并且 因此,显示启动屏幕,因此这需要更改 请检查这个答案。
如果用户不使用Chrome 72,显然应该使用回退功能,这对我来说完全依赖于设备:今天,如果用户版本的Chrome不支持可信的Web活动,Chrome将使用一个简单的工具栏,使用自定义选项卡。其他浏览器也可以实现可信Web活动使用的相同协议。虽然主机应用程序对打开的浏览器拥有最终决定权,但我们建议使用与自定义选项卡相同的策略:使用用户的默认浏览器,只要浏览器提供所需的功能,我已经安装了Chrome 76,并且仍然使用线程版本中建议的自定义标签客户端来解决这个问题