无法在片段内创建Web视图

时间:2019-07-02 04:26:53

标签: android xml android-studio kotlin

我一直试图在片段中加载Web视图,但是在线发布的任何方法都没有起作用,并且导致我的应用崩溃。我有一个包含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>

1 个答案:

答案 0 :(得分:0)

欢迎来到SO。

不能在FrameLayout中不应有两个子元素(它只能有一个)(它可以使用权重来处理多个子元素,以避免重叠,但是这之所以昂贵,是因为它需要多次度量/布局遍历,并且还因为有更好的方法来重叠视图或将多个视图放入ViewGroup中。

因此,用LinearLayout或更佳的ConstraintLayout替换片段中的FrameLayout。

另一个问题是您的活动中的以下代码段:

   <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(...),以确保在您告诉视图用户执行操作(例如加载网页)之前已创建视图。