【问题标题】:Cannot create a webview inside of a fragment无法在片段内创建 webview
【发布时间】:2019-07-02 04:26:53
【问题描述】:

我一直在尝试在片段中加载 webview,但网上发布的方法都没有奏效,导致我的应用程序崩溃。我有一个包含 5 个片段的底部导航菜单,我需要每个片段在 webview 中加载不同的网站。

以下是其中一个片段的 kotlin 文件:

class DaumFrag : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val v = inflater.inflate(R.layout.fragment_daum, container, false)
        val mWebView: WebView? = view?.findViewById(R.id.webviewdaum) as WebView
        mWebView?.loadUrl("https://www.daum.net/")

        val webSettings = mWebView?.getSettings()
        webSettings?.setJavaScriptEnabled(true)

        // Force links and redirects to open in the WebView instead of in a browser
        mWebView?.setWebViewClient(WebViewClient())
        return v
    }


}

下面是片段的xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".DaumFrag">

    <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="daum"/>
    <WebView
            android:layout_width="match_parent"
            android:layout_height="match_parent" android:id="@+id/webviewdaum"/>

</FrameLayout>

下面是mainactivity的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"
        tools:context=".MainActivity" android:orientation="horizontal">

    <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/BottomTab"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent" app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintVertical_bias="1.0">
        <WebView
                android:layout_width="match_parent"
                android:layout_height="match_parent" android:id="@+id/webview"/>
    </FrameLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:menu="@menu/bottom_navigation"
            android:background="?android:attr/windowBackground"
            app:itemTextColor="#000000"
            app:labelVisibilityMode="labeled"
            android:layout_gravity="bottom"
            android:id="@+id/BottomTab"
            app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="1.0" android:layout_marginEnd="8dp"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginRight="8dp" android:layout_marginStart="8dp"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginLeft="8dp"/>


</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 如果您发布崩溃日志的相关部分会有所帮助,我们真的不知道是什么导致您的应用崩溃。
  • 创建视图后尝试加载网页。

标签: android xml android-studio kotlin


【解决方案1】:

欢迎来到 SO。

不能在FrameLayout中不应该有两个孩子(它只能有一个)(它可以使用权重处理多个-以避免重叠-但是这个代价高昂,因为它需要多次测量/布局传递,还因为有更好的方法可以重叠视图或将多个视图放入 ViewGroup)。

所以用LinearLayout 或更好的ConstraintLayout 替换片段中的FrameLayout。

另一个问题是您活动中的这个 sn-p:

   <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/BottomTab"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent" app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintVertical_bias="1.0">
        <WebView
                android:layout_width="match_parent"
                android:layout_height="match_parent" android:id="@+id/webview"/>
    </FrameLayout>

从那里删除那个 webview,如果那是片段容器,你要把你的片段视图放在那里,所以你不能有两个孩子(你的片段和 webview)。您的 Fragment XML(上图)已经有 WebView。您不需要同时使用两个。

此外,将所有出现的LeftRight 分别替换为StartEnd,以更好地支持RTL 语言。例如。 constraintRight_ToRightOf 应该变成 constraintEnd_ToEndOf 等等。

不过,我会将所有创建后代码移至 onViewCreated(...),以确保在您告诉他们执行操作(例如加载网页)之前已创建您的视图。

【讨论】:

  • FrameLayout 可以容纳多个视图,但不是首选。很难组织起来。
  • 我知道 FrameLayout 可以做什么,但我告诉您不要这样做,因为这是一个非常糟糕的主意;为什么你想做一些文档告诉你不要做的事情,我引用:“FrameLayout 旨在阻止屏幕上的一个区域以显示单个项目。通常,应该使用 FrameLayout 来保存单个子视图,因为很难以一种可扩展到不同屏幕尺寸的方式组织子视图,而不会使子视图相互重叠。但是,您可以将多个子视图添加到 FrameLayout ......“这是非常低效的(权重)。不要这样做。
猜你喜欢
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多