2020年12月21日星期一

WebView in fragment doesnt load any webpage, kotlin, android studio

I am trying to load webpages in fragments to use in my new app, the problem is the code runs but for every website it shows me a white screen, no crashing or error messages. I am also trying to do this in Kotlinwhich might not be a good idea...But currently still have the time to port to 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)      }    }  

Thanks for any help in advance!

https://stackoverflow.com/questions/65401994/webview-in-fragment-doesnt-load-any-webpage-kotlin-android-studio December 22, 2020 at 09:03AM

没有评论:

发表评论