gpt4 book ai didi

android - fragment ViewPager NullPointer 问题

转载 作者:行者123 更新时间:2023-11-29 15:02:44 26 4
gpt4 key购买 nike

我有一些 fragment 在我的主要 Activity 中被换入和换出。初始 fragment 称为 BrowseHomeFragment 并且最初加载正常,但是当我用新 fragment 替换它然后将其替换回来时,我收到以下错误

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:686)
at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:215)
at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:536)

BrowseHomeFragment的onCreateView方法:

public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
InjectionGraph.inject(this);

View view = inflater.inflate(R.layout.fragment_browse, container, false);
ButterKnife.inject(this, view);

addTabsToPagerSlidingTabStrip(inflater);

viewPager.setAdapter(new BrowseHomePagerAdapter(getChildFragmentManager()));

pagerSlidingTabStrip.setViewPager(viewPager);
// has to set page change listener in indicator
pagerSlidingTabStrip.setOnPageChangeListener(pageChangeListener);

viewPager.setCurrentItem(initialTabIndex);

return view;
}

BrowseHomePagerAdapter

public class BrowseHomePagerAdapter extends FragmentStatePagerAdapter {
private int lastPosition = -1;

@Inject
BrowseHomepageMetricRecorder browseHomepageMetricRecorder;

@Inject
NetworkMonitor networkMonitor;


/**
* For explanation on why Fragment references are cached, please refer to
* http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager
*/
final SparseArray<Fragment> registeredFragments = new SparseArray<>();


public BrowseHomePagerAdapter(FragmentManager fm) {
super(fm);
InjectionGraph.inject(this);

}


@Override
public Fragment getItem(int i) {

switch (BrowseHomeFragment.Tab.resolve(i)) {
// TODO: connect category, favorite, and downloaded list fragments
case Categories:
return new CategoryHomePageFragment();
case Downloaded:
return new OfflineContentFragment();
default:
return new ComingSoonFragment();
}
}


@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}


@Override
public int getCount() {
return BrowseHomeFragment.Tab.values().length;
}


@Override
public CharSequence getPageTitle(int position) {
return BrowseHomeFragment.Tab.resolve(position).name();
}


@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
if (position != lastPosition) { // debounce Android's redundant calls
switch (BrowseHomeFragment.Tab.resolve(position)) {
case Categories:
browseHomepageMetricRecorder.recordGotoCategoriesMetric();
if (!networkMonitor.isDeviceConnected()) {
networkMonitor.postUserActionRequiresConnectivityEvent();
}
break;
case Downloaded:
browseHomepageMetricRecorder.recordGotoDownloadedMetric();
break;
default:
break;
}
lastPosition = position;
}

super.setPrimaryItem(container, position, object);
}


public Object instantiateItem(ViewGroup container, int position) {
final Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}


public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}

我收集到 viewPager 丢失了一些东西,尽管我已经运行调试并且它不为空..

我可以通过在每次用 BrowseFragment 替换 Activity fragment 之前简单地重新创建 BrowseFragment 来解决这个问题,但我宁愿能够重用它。

如果您能帮助理解到底发生了什么以及如何解决它,我们将不胜感激。

最佳答案

在敲了敲我的头之后,我找到了一个解决方案。在我的适配器中,我现在重写了 onRestoreState 什么也不做。

@Override
public void restoreState(Parcelable state, ClassLoader loader) {
//Do NOTHING;
}

我不确定这是否是最好的解决方案,似乎没有任何不良影响,如果有人可以谈谈这个“修复”以及它为什么有效或提供更好的解决方案,我们将不胜感激。

关于android - fragment ViewPager NullPointer 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41967318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com