ViewPager2没有使用POSITION_NONE更新片段。

huangapple go评论96阅读模式
英文:

ViewPager2 not updating fragment with POSITION_NONE

问题

我已经实现了ViewPager2,并且使用了两个选项卡。我希望能够使用以下代码在连续滑动之间刷新每个片段,但是这并没有起作用。

  1. @Override
  2. public int getItemCount() {
  3. return POSITION_NONE;
  4. }

它抛出了以下崩溃错误:

  1. E/AndroidRuntime: FATAL EXCEPTION: main
  2. Process: com.myapp, PID: 5842
  3. java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.material.tabs.TabLayout$Tab com.google.android.material.tabs.TabLayout$Tab.setIcon(int)' on a null object reference

请建议适当的方法来实现这一目标。

英文:

I have implemented ViewPager2 and implemented with 2 tabs. I want to refresh each fragment between consecutive swipe with the following code but it is not working.

  1. @Override
  2. public int getItemCount() {
  3. return POSITION_NONE;
  4. }

It throws following crash error:

  1. E/AndroidRuntime: FATAL EXCEPTION: main
  2. Process: com.myapp, PID: 5842
  3. java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.material.tabs.TabLayout$Tab com.google.android.material.tabs.TabLayout$Tab.setIcon(int)' on a null object reference

Please suggest appropriate way to accomplish this.

答案1

得分: 0

问题已解决,通过在片段生命周期的onResume方法中更新用户界面。

英文:

Solved, this issue by updating UI from onResume method of fragment life cycle.

huangapple
  • 本文由 发表于 2020年9月5日 19:21:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63753333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定