【发布时间】: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 是什么?添加片段的完整代码。