title: android - can't access view inside fragment method. it returns null object
body: <p>I'm building an app based on the tabbedActivity preset of android studio ,therefore fragments are created so:</p>
  
  <pre><code>private SectionsPagerAdapter mSectionsPagerAdapter;
  
      private ViewPager mViewPager;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
          // Create the adapter that will return a fragment for each of the three
          // primary sections of the activity.
          mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
  
          // Set up the ViewPager with the sections adapter.
          mViewPager = (ViewPager) findViewById(R.id.container);
          mViewPager.setAdapter(mSectionsPagerAdapter);
  
          TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
          tabLayout.setupWithViewPager(mViewPager);
  }
  </code></pre>
  
  <p>and later on:</p>
  
  <pre><code>public class SectionsPagerAdapter extends FragmentPagerAdapter {
  
          public SectionsPagerAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public Fragment getItem(int position) {
              switch (position) {
                  case 0:
                      DashboardTab tabDashboard = new DashboardTab();
                      return tabDashboard;
                  case 1:
                      VertretungsplanTab tabVertretungsplan = new VertretungsplanTab();
                      return tabVertretungsplan;
                  default:
                      return null;
              }
          }
  
          @Override
          public int getCount() {
              // Show 2 total pages.
              return 2;
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
              switch (position) {
                  case 0:
                      return "Dashboard";
                  case 1:
                      return "Vertretungsplan";
              }
              return null;
          }
      }
  </code></pre>
  
  <p>this is all working fine but in fact it is quite an uncommon way of creating tabs so it might be that my problem is caused by a missunderstanding of the MainActivity.java</p>
  
  <pre><code>public class VertretungsplanTab extends Fragment {
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.vertretungsplan_fragment, container, false);
          return rootView;
      }
  
  
  
      public void setText(String textToBeSet, VertretungsplanTab tab){
          TextView tv = (TextView) getView().findViewById(R.id.textView2);
      }
  </code></pre>
  
  <p>now the problem is as far as i now caused by the view, because it is a null object inside my method.</p>
  
  <pre><code>Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
  </code></pre>
  
  <p>I've tried several ways of accessing the view, which includes creating a new variable in this fragment and commiting the view to it in onCreateView().
  Also if I checked it in onCreatedView() it wasn't yet a null object.</p>
  
  <p>I've also tried to create the TextView in onCreateView(), but after onCreatedView() it isn't possible to call setText()</p>
  
  <p>After searching the internet several days it would be a great help if a more advanced developer could get a solution!</p>