0

I checked all the similar tickets with "getSupportFragmentManager().findFragmentById returns null" but none helped.

In Android Studio 3.0.1 I just created a new project based on the "Tabbed Activity" template project and navigation style set to "Action Bar Tabs (with ViewPager)".

Then I added 3 xml files

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment1">
<!-- @+id/fragment2 in fragment2.xml-->
<!-- @+id/fragment3 in fragment3.xml-->
...some controls
</LinearLayout>

And the 3 corresponding .java class files

public class Fragment1 extends Fragment {
   //Fragment2, Fragment3

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment1, container, false);
        //R.layout.fragment2, R.layout.fragment3 
    }

}

In the SectionsPagerAdapter class of MainActivity class, I changed the overriden method getItem to the following.

@Override
public Fragment getItem(int position) {
     // getItem is called to instantiate the fragment for the given page.
     // Return a PlaceholderFragment (defined as a static inner class below).
     switch(position) {
         case 0: return new Fragment1();
            case 1: return new Fragment2();
            case 2: return new Fragment3();
            default:
                return new Fragment1();

     }
     //return PlaceholderFragment.newInstance(position + 1);
}

And finally, I want to trigger something in my first fragment.

 public void onClick(View view) {
       Fragment1 frag1 = (Fragment1) getSupportFragmentManager().findFragmentById(R.id.fragment1);
 }

My problem is that when I click a button in the toolbar and I reach its onClick event (in MainActivity), my frag1 is always null!

Please help! :)

kevinob
  • 558
  • 2
  • 6
  • 14
  • 2
    The Fragments need to be read from the PagerAdapter, not the FragmentManager – OneCricketeer Dec 03 '17 at 03:55
  • Possible duplicate of [Retrieve a Fragment from a ViewPager](https://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-a-viewpager) – ADM Dec 03 '17 at 04:44

1 Answers1

0

you shouldn't override getItem like this way

I suggest to be like:

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    private List<Fragment> fragments;

    public SectionsPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    public void setFragments(List<Fragment> fragments) {
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {
        return this.fragments.get(position);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }


}

and if you want to get any fragment you want just call

((FragmentCallBack)sectionsPagerAdapter.get(thePositionOfYourFragment))
.trigger();
MusabAlothman
  • 68
  • 1
  • 8
  • ok but where the fragments should be initialized? oops, in the constructor! sorry! thx – kevinob Dec 03 '17 at 13:54
  • 1
    where do `FragmentCallBack` and `trigger` come from? – kevinob Dec 03 '17 at 14:52
  • "FragmentCallBack" could be any callbacks you are using or it could be the fragment name, even the "trigger" function it could be any function that you are using to trigger your fragment. You didn't share the hole code that's way I write these things to make it more clear to you – MusabAlothman Dec 04 '17 at 14:30