我正在尝试以 fragment 形式加载网页以在我的新应用程序中使用,问题是代码运行但对于每个网站它都显示白屏,没有崩溃或错误消息。我也在尝试在 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>
第一个 fragment .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)
}
}
提前感谢您的帮助!
最佳答案
将 WebView 部分代码放在 onViewCreated 而不是 onCreateView 中,在 onCreateView 中只是膨胀 fragment 的 x xml 布局并返回 View 。 在 onCreateView 函数创建 View 后,应在 onViewCreated 中对 fragment 容器执行操作。
所以你修改后的代码将是:
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
关于android - fragment 中的 WebView 不加载任何网页、kotlin、android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65401994/