【问题标题】:WebView in fragment doesnt load any webpage, kotlin, android片段中的 WebView 不加载任何网页、kotlin、android
【发布时间】:2020-12-22 01:03:52
【问题描述】:

我正在尝试以片段的形式加载网页以在我的新应用程序中使用,问题是代码运行,但对于每个网站,它都会向我显示白屏,没有崩溃或错误消息。我也在尝试在 Kotlin 中执行此操作,这可能不是一个好主意……但目前仍有时间移植到 java AndroidManiferst.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.braun.testingwebview">
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.TestingWebView">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.TestingWebView.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

fragment_first.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".FirstFragment">

    <TextView
        android:id="@+id/textView"
        android:layout_width="213dp"
        android:layout_height="108dp"
        android:text="Test"
        tools:ignore="MissingConstraints" />

    <WebView
        android:id="@+id/WebView1"
        android:layout_width="match_parent"
        android:layout_height="600dp"
        app:layout_constraintBottom_toBottomOf="parent"/>

</LinearLayout>

FirstFragment.kt

package com.braun.testingwebview

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import kotlinx.android.synthetic.main.fragment_first.*

class FirstFragment : Fragment() {

    @SuppressLint("SetJavaScriptEnabled")
    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val myWebView: WebView = WebView1
        myWebView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(
                view: WebView,
                url: String
            ): Boolean {
                view.loadUrl(url)
                return true
            }
        }

        myWebView.loadUrl("https://google.com")
        myWebView.settings.javaScriptEnabled = true
        myWebView.settings.allowContentAccess = true
        myWebView.settings.domStorageEnabled = true
        myWebView.settings.useWideViewPort = true
        return inflater.inflate(R.layout.fragment_first, container, false)
    }

}

提前感谢您的帮助!

【问题讨论】:

  • 这里的 WebView1 是什么?添加片段的完整代码。

标签: android kotlin webview


【解决方案1】:

将WebView部分代码放在onViewCreated而不是onCreateView中,在onCreateView中只是膨胀fragment'x xml布局并返回视图。 在 onCreateView 函数创建视图后,应在 onViewCreated 中对片段容器进行操作。

所以您修改后的代码将是:

override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false)
    }

override fun onViewCreated(view: View, savedInstanceState: Bundle?){
val myWebView: WebView = view.findViewById(R.id.WebView1)
        myWebView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(
                view: WebView,
                url: String
            ): Boolean {
                view.loadUrl(url)
                return true
            }
        }

    myWebView.loadUrl("https://google.com")
    myWebView.settings.javaScriptEnabled = true
    myWebView.settings.allowContentAccess = true
    myWebView.settings.domStorageEnabled = true
    myWebView.settings.useWideViewPort = true

}

【讨论】:

  • 我试过了,但是还是白屏
  • 尝试加载内容而不是 URL,然后检查它是否正常工作?
  • 对不起,你能详细说明一下什么样的内容吗?
  • 不幸的是没有运气,仍然没有加载
【解决方案2】:

这对我有用

 class LogoutFragment : Fragment() {


    private var _binding: FragmentLogoutBinding? = null
    private val binding get() = _binding!!


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        _binding = FragmentLogoutBinding.inflate(inflater, container, false)
        val root: View = binding.root

        return root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val myWebView: WebView = view.findViewById(R.id.WebView)
        myWebView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(
                view: WebView,
                url: String
            ): Boolean {
                view.loadUrl(url)
                return true


            }


        }
        val webSetting:WebSettings = myWebView.settings
        webSetting.javaScriptEnabled = true
        myWebView.webViewClient = WebViewClient()

        myWebView.canGoBack()
        myWebView.setOnKeyListener(View.OnKeyListener { v , keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_BACK

                && event.action == MotionEvent.ACTION_UP
                && myWebView.canGoBack()){
                myWebView.goBack()
                return@OnKeyListener true
            }
            false
        })


        myWebView.loadUrl("https://youtube.com")
        myWebView.settings.javaScriptEnabled = true
        myWebView.settings.allowContentAccess = true
        myWebView.settings.domStorageEnabled = true
        myWebView.settings.useWideViewPort = true


    }

}

【讨论】:

  • edit 提供更多详细信息,说明您所做的更改、更改的原因以及如何解决问题。
猜你喜欢
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
  • 1970-01-01
  • 2017-02-21
  • 2013-02-02
相关资源
最近更新 更多