【问题标题】:Trying to add a Refresh Method to a WebView and having issues尝试向 WebView 添加刷新方法并遇到问题
【发布时间】:2021-11-10 21:50:11
【问题描述】:

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

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


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>

【问题讨论】:

  • 如果在webView.reload(); 之后添加swipe.setRefreshing(false); 会发生什么?
  • 仍然关闭。 Logcat:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void androidx.swiperefreshlayout.widget.SwipeRefreshLayout.setOnRefreshListener(androidx.swiperefreshlayout.widget.SwipeRefreshLayout$OnRefreshListener)”
  • 我想它指的是这个? swipe = (SwipeRefreshLayout) container.findViewById(R.id.swipe); swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  • swipe = (SwipeRefreshLayout) container.findViewById(R.id.swipe); swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() - 不!没关系

标签: java android webview


【解决方案1】:

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
            }
        });

【讨论】:

  • 谢谢!!我无法点击“这个答案很有用,因为我今天必须创建一个新帐户才能登录,但感谢一百万!我花了至少 26 个小时查看不同的代码,试图找出它为什么会关闭!下载吨Github 上的示例,但没有任何帮助!我现在明白为什么 Google 开发人员在 Google I/O 上说 Stack Over Flow 已经过时,问题太老了,无法回答现在的问题!
  • 没问题。很高兴帮助:)
  • 现在,如果我可以将new Handler().postDelayed(new Runnable() 添加到整个事情中,我就完成了:) 但它的工作方式就是这样。当我添加它时,它再次关闭。不知道我是否应该将其作为一项活动,或者这是否重要。
猜你喜欢
  • 2012-12-17
  • 2017-07-28
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-05
  • 1970-01-01
  • 2020-07-07
相关资源
最近更新 更多