gpt4 book ai didi

Android WebView 即时崩溃与 shouldInterceptRequest 覆盖

转载 作者:太空狗 更新时间:2023-10-29 13:03:48 29 4
gpt4 key购买 nike

我在 Android WebView 上玩得很开心。

我用它来显示登录屏幕,然后在响应时拦截授权代码。应该很简单...

如果我只覆盖 shouldOverrideUrlLoading 但如果我覆盖(就像 Android Studio 自动完成的那样),我的 WebView 加载和显示绝对正常:

override fun shouldInterceptRequest(
view: WebView?,
request: WebResourceRequest?
): WebResourceResponse {
return super.shouldInterceptRequest(view, request)
}

如果没有其他更改,它会在运行时立即崩溃并发生 native 崩溃

A/chromium: [FATAL:jni_android.cc(259)]

其次是

A/libc: Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 16220 (TaskSchedulerFo), pid 16175 (eports.internal)

奇怪的是,如果我使响应可为空,WebView 将再次工作。但是,将任何其他内容添加到 shouldInterceptRequest 方法中都会使其因相同的错误而失败。

所以这是可行的:

override fun shouldInterceptRequest(
view: WebView?,
request: WebResourceRequest?
): WebResourceResponse? {
return super.shouldInterceptRequest(view, request)
}

但这会因上述崩溃而崩溃:

override fun shouldInterceptRequest(
view: WebView?,
request: WebResourceRequest?
): WebResourceResponse? {
val url = view?.url
return super.shouldInterceptRequest(view, request)
}

这似乎是一个非常奇怪的问题,我不明白为什么添加一个 val 赋值会有任何不同。

我一直在研究错误,建议补充

webView.destroy()

在 Activity/fragment onDestroy/onDestroyView 中,不幸的是,这没有帮助。

行为在设备和模拟器上以及在 Android sdk 22 和 28 上是相同的。

有没有人见过这样的事情?我觉得我可能遗漏了一些明显的东西。

如果它对任何人都有用,我也生成了 Breakpad Microdump,它太大而无法在这个问题中发布。但如果它或它的一个子集可能有助于诊断,请告诉我!

最佳答案

我在我的案例中发现了这个问题,所以我想我会把它发布在这里以供处于类似位置的其他人使用。

native 崩溃是由 WebView 中运行的 Javascript 引起的。

WebView 在后台线程中运行 JavaScript,因此任何接触 UI 线程的东西都会导致它在 native 级别上失败。给出上面非常无用的崩溃。

val 分配导致崩溃的原因不是 val 的分配,而是 if view?.url 的调用,它在 UIThread 上。

解决方案?

Kotlin :

webView?.post{
// Do your UI work here.
}

Java:

webView.post(new Runnable(){
public void run() {
// Do your UI work here.
}
})

并且不要在覆盖的方法中以其他方式触及 UIThread!

关于Android WebView 即时崩溃与 shouldInterceptRequest 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51358223/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com