Actually I'm using Fragments with FragmentPagerAdapter, but I have the problem which is null when i tried to access views again in fragment. pageNum is null as well. I tried many ways to figure it out but it didn't work though.
FirstFragment.java
public class FirstFragment extends Fragment{
private int pageNum;
private TextView titleTxt;
private LinearLayout topPan;
public static FirstFragment newInstance(int page) {
FirstFragment fragment = new FirstFragment();
Bundle args = new Bundle();
args.putInt("page", page);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageNum = getArguments().getInt("page", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.workflow_item_layout, container, false);
titleTxt = (TextView) view.findViewById(R.id.title);
topPan = (FrameLayout) view.findViewById(R.id.top_pan);
}
public void setTopPanVisible(boolean isVisible) {
topPan.setVisibility(isVisible);
}
pageChangeListener codes
private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
final int position = pager.getCurrentItem();
if (state == ViewPager.SCROLL_STATE_IDLE) {
final FirstFragment fragment = (FirstFragment) adapter.getItem(position);
fragment.setTopPanVisible(true);
topPan is null when i called 'setTopPanVisible' in pageChangeListener. How can Views keep alive to have a value?
If you are getting a null pointer exception, then that means one of two things:
setTopPanVisible()
before the fragment'sonCreateView()
method has been called.view.findViewById(R.id.top_pan);
.In order to fix the problem, you should figure out which one of these problems you are facing.