尝试将刷新方法添加到 WebView 并遇到问题

Trying to add a Refresh Method to a WebView and having issues

我正在尝试向网络视图布局添加简单的滑动刷新。我到处搜索并尝试了与我在 GitHub 和 Android 开发人员页面上找到的方法不同的方法,但似乎没有任何效果。当我使用代码中没有 OnRefresh 方法的代码加载网页时,它加载得很好。但是一旦我启用该方法,页面就会关闭应用程序。任何建议都会很棒,因为我是 android(1 年经验)的新手并且正在努力学习。谢谢

这是我正在使用的代码示例。希望我每次在这里提问时都不会 post 提供太多信息,我被告知我 post 太多了!!


private AboutViewModel AboutViewModel;
SwipeRefreshLayout swipe;

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View root;
    AboutViewModel = new ViewModelProvider(this).get(AboutViewModel.class);
        root = inflater.inflate(R.layout.fragment_about, container, false);

        WebView webView = root.findViewById(R.id.web_view_about);
        webView.loadUrl("https://example.com");
        webView.setWebViewClient(new WebViewController());

    swipe = (SwipeRefreshLayout) container.findViewById(R.id.swipe);
    swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            webView.reload();
        }
    });
    return root;
}

这是布局xml

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/web_view_about"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

root 替换容器,如下所示:

swipe = root.findViewById(R.id.swipe);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                webView.reload();
                swipe.setRefreshing(false); // Call this at the end of loading
            }
        });