我正在处理web视图,但按下后退按钮时应用程序崩溃,进度条没有显示100%它打开了一个网站,也不支持javascript提醒框?这是我的代码
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
view?.loadUrl(url)
setting!!.domStorageEnabled= true
setting!!.javaScriptEnabled = true
return true
}
}
// Get the web view settings instance
val settings = webview.settings;
// Enable java script in web view
settings.javaScriptEnabled = true
// More optional settings, you can enable it by yourself
settings.domStorageEnabled = true
settings.setSupportMultipleWindows(true)
settings.loadWithOverviewMode = true
settings.allowContentAccess = true
settings.setGeolocationEnabled(true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
settings.allowUniversalAccessFromFileURLs = true
}
settings.allowFileAccess = true
// WebView settings
webview.fitsSystemWindows = true
/* if SDK version is greater of 19 then activate hardware acceleration
otherwise activate software acceleration */
webview.setLayerType(View.LAYER_TYPE_HARDWARE, null)
webview.loadUrl("https://www.google.com/")
override fun onBackPressed() {
if (webview!!.canGoBack()) {
// If web view have back history, then go to the web view back history
webview?.goBack()
}else{
super.onBackPressed()
}
}
Logcat这样说:
E/InputEventSender: Exception dispatching finished signal.
E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
E/MessageQueue-JNI: kotlin.KotlinNullPointerException
at com.work.social.MainActivity.onBackPressed(MainActivity.kt:134)
从onBackPressed((中删除代码
关于JS提醒框,你在真实设备上测试过吗?
我非常怀疑onBackPressed()
中的本地!!
。
override fun onBackPressed() {
when {
webview?.canGoBack() == true -> webview.goBack()
else -> super.onBackPressed()
}
}
在大多数情况下,调用!!
是个坏主意。和KotlinNullPointerException
的源(例如:Kotlin试图将T?强制转换为T,如果为null,则失败(。