title: Communicate between Activity and ViewPager Fragments
body: <p>Like in the image below, I have an <code>Activity</code> with a <code>ViewPager</code> (with <code>TabLayout</code>) inside.
  Now I want to refresh for example the text of an <code>TextView</code> in a <code>ViewPager</code> <code>Fragment</code>. How can I do this?</p>
  
  <p><a href="http://imgur.com/0kWQCfx" rel="nofollow">http://imgur.com/0kWQCfx</a>
  [Sorry for the image link, but I don't have enough reputations yet.]</p>
  
  <p>I already tried something like this:</p>
  
  <pre><code>//In Activity
  View root = getLayoutInflater().inflate(R.layout.fragment_home, null);
  TextView textView = (TextView) root.findViewById(R.id.text_view);
  textView.setText("test");
  </code></pre>
  
  <p>But I get a <code>NullPointerException</code> for the textView.</p>
  
  <p>Edit 1:
  I only need to access the components inside one Fragment, so I am not using an array inside the Adapter.
  I changed my <code>ViewPagerAdapter</code> to this:</p>
  
  <pre><code>public class ViewPagerAdapter extends FragmentPagerAdapter {
      public HomeFragment fragment;
  
      ...
  
      @Override
      public Fragment getItem(int position) {
          switch (position) {
              case 0:
                  return fragment = new HomeFragment();
              case 1:
                  ...
              default:
                  return null;
          }
      }
  </code></pre>
  
  <p>But I still get a <code>NullPointerException</code> here:</p>
  
  <pre><code>final Fragment root = viewPagerAdapter.fragment;
  Log.d("testtest", String.valueOf(root == null));
  </code></pre>