I have an application with ViewPager that contains two fragments: the main fragment and the list fragment. If something is selected in the list, an event occurs that is processed in this action. The action switches to the main fragment and calls the setSelectedItem method. In this method, I use getView () to search for a specific view to change the text to fit the selected item.
In most cases this works, but sometimes getView returns null, and I don't know why.
The activation of the pagerAdapter is created in the OnCreate activity:
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager());
ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
pager.setAdapter(this.mPagerAdapter);
The pager adapter instantiates both fragments in its constructor:
public PagerAdapter(FragmentManager fm) {
super(fm);
this.main = MainFragment.newInstance();
this.list = ListFragment.newInstance();
}
This is the method in activity that is called when an item is selected from the list:
ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager);
pager.setCurrentItem(0, true);
((MainFragment)mPagerAdapter.getItem(0)).setSelectedItem(id);
And finally, this is the line in the setSelectedItem method in MainFragment that causes the problem:
TextView s = (TextView)getView().findViewById(R.id.CurrentSelection);
, , .