WebView cannot load the web page, it is blank

When I loaded the webpage on the project today, I found that one could not be loaded, emmm, I looked at the previous projects, and found that there were more than one omissions, so I will make a summary here.

1. Permission configuration: Make sure to add network permissions in the AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />

2. WebView settings: Check the relevant settings of WebView to ensure that JavaScript is enabled and the ability to load remote content:

webView.settings.javaScriptEnabled = true
webView.settings.loadsImagesAutomatically = true
Some projects can be loaded without adding this before, but it’s okay to add it

3. Listen for errors: Create a WebViewClient and set it to WebView to handle page loading and errors:

webView.webViewClient = object : WebViewClient() {
    override fun onPageFinished(view: WebView?, url: String?) {
        // 页面加载完成后的处理
    }

    override fun onReceivedError(
        view: WebView?,
        errorCode: Int,
        description: String?,
        failingUrl: String?
    ) {
        // 页面加载错误的处理
    }
}
 
 

4. Check the network connection: Make sure the device is connected to the Internet normally and can access the loaded webpage.

   Check web links: Make sure that the loaded web links are correct and there are no redirects or access restrictions.

my code:


class PrivacyActivity : AppCompatActivity() {

    private lateinit var webView: WebView
    private lateinit var close: AppCompatImageView

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_privacy)

        ImmersionBar.with(this).fullScreen(false).navigationBarColor(R.color.transparent)
            .statusBarColor(R.color.transparent).init()

        webView = findViewById(R.id.icon_webview)
        close = findViewById(R.id.icon_back)

        webView.canGoBack()
        webView.settings.javaScriptEnabled = true
        webView.settings.loadsImagesAutomatically = true

        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(
                view: WebView?, request: WebResourceRequest?
            ): Boolean {
                view?.loadUrl("https://blog.csdn.net/LoveFHM?type=blog")
                return true
            }
        }
        webView.loadUrl("https://blog.csdn.net/LoveFHM?type=blog")

        close.setOnClickListener { finish() }

    }

    override fun onDestroy() {
        webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null)
        webView.clearHistory()
        (webView.parent as ViewGroup).removeView(webView)
        webView.destroy()
        super.onDestroy()
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack()
            return true
        }
        return super.onKeyDown(keyCode, event);
    }
}

Guess you like

Origin blog.csdn.net/LoveFHM/article/details/131558461