2

Im using 3 fragments in my ViewPager adapter. I will be loading data from Parse(parse.com) and displaying them in recycler views. The following code is causing my app to crash. What my understanding is that when my MainActivity loads, this is my first fragment ie, it gets viewed by user immediately so the setUserVisibleHint function gets called immediately and in that v.findViewById code causes a null pointer exception since setContentView hasnt/ may not been called. My proof for this is if i add a 1sec delay to setUserVisisbleHint then my code works properly.

Now I want to add server PULL requests using Parse, add data in a list to an adapter and populate recyclerview AFTER user views the page so

1) Should i add all the code in setUserVisisbleHint and just add a 0.5secc delay so it gets executed after setContentView is called ensuring I dont get a null pointer exception error OR

2) Is there a better way/ other functions I can use to achieve the same?

public class NewsFeed extends Fragment {
        LinearLayoutManager mLayoutManager;
        boolean _areLecturesLoaded=false;
        View v;
        ProgressBar bar;

        public NewsFeed() {
            // Required empty public constructor
        }

        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                                 @Nullable Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            v=inflater.inflate(R.layout.fragment_news_feed, container, false);

            RecyclerView mRecyclerView = (RecyclerView)v.findViewById(R.id.recycler_view);
            mRecyclerView.setHasFixedSize(true);
            mLayoutManager = new LinearLayoutManager(v.getContext());
            mRecyclerView.setLayoutManager(mLayoutManager);    
            MyStickyAdapter mAdapter = new MyStickyAdapter(v.getContext());    
            mRecyclerView.setAdapter(mAdapter);
           // mRecyclerView.addItemDecoration(new StickyRecyclerHeadersDecoration(mAdapter));
            return v;
        }

        @Override
        public void setUserVisibleHint(boolean isVisibleToUser) {
            super.setUserVisibleHint(isVisibleToUser);
            if (isVisibleToUser && !_areLecturesLoaded ) {
                _areLecturesLoaded = true;
                v.findViewById(R.id.asd).setVisibility(View.GONE);
            }
        }


    }//Closes Fragment

Im using this library for my recycler view StickyHeaderRecyclerView

Varun Agarwal
  • 1,587
  • 14
  • 29

1 Answers1

0

I would highly recommend avoiding adding delays especially if you're running this on the main thread. To avoid the NPE, try moving the findViewById(R.id.asd) into your onCreateView method after you inflate the view:

@Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        v=inflater.inflate(R.layout.fragment_news_feed, container, false);
        v.findViewById(R.id.asd).setVisibility(View.GONE);

This is assuming that R.id.asd is in R.layout.fragment_news_feed

This post could also provide some insight: setUserVisibleHint called before onCreateView in Fragment

Community
  • 1
  • 1
Vinit Nayak
  • 103
  • 1
  • 9
  • This fragment gets viewed instantly. In that case the progress bar may be referenced before the onCreate is completed. This again gives rise to null pointer exception. – Varun Agarwal Aug 16 '15 at 17:18
  • You can't really get around accessing elements in a view before the view is inflated. Is it possible to move whatever view manipulation you are doing to after the view is inflated? – Vinit Nayak Aug 17 '15 at 20:05
  • The first fragment is always loaded when the app starts, So I removed the setUserVisbleHunt from that as its redundant and put it for the other fragments where its working fine. – Varun Agarwal Aug 18 '15 at 06:50
  • Yes the other fragment's onCreate always gets completed before the user is able to swipe to them so in that case there is no crash and for the first fragment, I have removed setUserVisibleHint all together as its not really needed, so that doesnt cause a crash either. The problem is you cant move the manipulation after the view is inflated as setUserVisibleHint is a separate function itself with @Override so its called automatically once the app starts. Thanks for your help though. – Varun Agarwal Aug 19 '15 at 07:15