Android Kotlin WebView加载光污染地图不显示问题求助
在Kotlin开发的Android应用中,使用WebView加载光污染地图网站时,仅能显示按钮等附加元素,无法加载带亮度差异的地图。Logcat报错信息如下:
I/chromium: [INFO:CONSOLE(2)] "jQuery.Deferred exception: Cannot read property 'getItem' of null TypeError: Cannot read property 'getItem' of null
at init (lightpollutionmap.js:28:351)
at HTMLDocument. (lightpollutionmap.js:19:35)
at e (jquery-3.5.1.min.js:2:30005)
at t (jquery-3.5.1.min.js:2:30307) undefined", source: jquery-3.5.1.min.js (2)
I/chromium: [INFO:CONSOLE(2)] "Uncaught TypeError: Cannot read property 'getItem' of null", source: jquery-3.5.1.min.js (2)
I/chromium: [INFO:CONSOLE(407)] "Uncaught TypeError: Cannot read property 'getItem' of null", source: lightpollutionmap.js (407)
D/ProfileInstaller: Installing profile for com.example.lightpollution
I/chromium: [INFO:CONSOLE(400)] "Uncaught (in promise) TypeError: Cannot read property 'getItem' of null", source: lightpollutionmap.js (400)
- 启用JavaScript支持:该网站依赖JavaScript渲染地图,WebView默认禁用此功能,需手动开启:
webView.settings.javaScriptEnabled = true - 开启DOM存储API:报错中的
getItem调用来自DOM存储(localStorage/sessionStorage),WebView默认关闭,需开启:webView.settings.domStorageEnabled = true - 配置数据库存储(可选):部分网站功能可能依赖数据库存储,可补充配置:
webView.settings.databaseEnabled = true val dbPath = applicationContext.filesDir.absolutePath + "/databases" webView.settings.databasePath = dbPath - 设置WebViewClient:避免页面跳转至系统浏览器,确保地图在WebView内加载:
webView.webViewClient = object : WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { request?.url?.let { view?.loadUrl(it.toString()) } return true } } - 检查定位权限:光污染地图可能需要定位权限,确保应用已申请
ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION权限,且用户已授权。
内容的提问来源于stack exchange,提问作者pranish Kafle

