Ethan Shoe
Ethan Shoe

Reputation: 574

Fragment View is Null When Called From Parent Activity

I have an activity and its child Fragment with a LinearLayout that I generate buttons inside of. When the fragment is created, everything runs fine. However, when the parent activity downloads a new item, I call the method in the fragment that is used to generate the buttons and add them to the view, but the LinearLayout returns null and I can't figure out why. I either need to fix it or find a way to "re-display" my fragment. Here is the related code:

SongFragment:

LinearLayout linearLayout;
DatabaseHelper databaseHelper;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_song, container, false);
    linearLayout = rootView.findViewById(R.id.songFragmentMainLayout);
    databaseHelper = new DatabaseHelper(getActivity());

    return rootView;
}

@Override
public void onResume() {
    super.onResume();

    RefreshButtons();
}

public void RefreshButtons(){
    linearLayout.removeAllViews(); //this line is where the NullPointerException is called
    ...
}

MainActivity:

                    //refresh fragment view
                    SongFragment fragment = (SongFragment) sectionsPagerAdapter.getItem(0);
                    if(downloadQueue.size() == 0){
                        fragment.RefreshButtons();
                        Toast.makeText(context, "New songs downloaded", Toast.LENGTH_SHORT).show();
                    }

...

public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case 0:
                    fragment = new SongFragment();
                    break;
                case 1:
                    fragment = new PlaceholderFragment();
                    break;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Songs";
                case 1:
                    return "Playlists";
            }
            return null;
        }
    }

Thanks for your help.

Upvotes: 1

Views: 791

Answers (1)

Vince VD
Vince VD

Reputation: 1581

The method fragment.RefreshButtons(); returns an NPE because if you implemented SectionsPagerAdapter like you should, getItem() returns a new Instance of that fragment which is not yet attached to the fragment manager, therefore causing a Nullpointer exception.

So what you should do is get a currently active fragment instance like this:

 Fragment frag = (Fragment) mViewPager.getAdapter().instantiateItem(mViewPager, 0);

0 is the position of your fragment, so for example if you have 3 fragments, 0 will return the first fragment instance etc...

Upvotes: 2

Related Questions