title: Get view from fragment in activity
body: <p>I'm trying to get view from Fragment dynamically added to Activity. </p>
  
  <p>MainActivity.java</p>
  
  <pre><code>   @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      ...
  
      if (savedInstanceState == null) {
          // on first time display view for first nav item
          displayView(0);
  
      }
  
      myDrawerList.setOnItemClickListener(new DrawerItemClickListener());
      handleIntent(getIntent());
  
  }
  
  private class DrawerItemClickListener implements ListView.OnItemClickListener {
      @Override
      public void onItemClick(
          AdapterView&lt;?&gt; parent, View view, int position, long id
      ) {
          // display view for selected nav drawer item
          displayView(position);
      }               
  }
  
  private void displayView(int position) {
      // update the main content by replacing fragments
      Fragment fragment = null;
      String userToken = UserTokenStorageFactory.instance().getStorage().get();
      switch (position) {
      case 0:
          fragment = new AllCategories();
          break;
      case 1:
          fragment = new Subscription();
          break;
      case 2:
          if( userToken != null &amp;&amp; !userToken.equals( "" ) ){
              Intent myIntent1 = new Intent(this, PrivateOffice.class);
              this.startActivity(myIntent1);
              break;
          }
          else {
              Intent myIntent1 = new Intent(this, LogIn.class);
              this.startActivity(myIntent1);
              break;
          }
      case 3:
          if( userToken != null &amp;&amp; !userToken.equals( "" ) ){
              Intent myIntent1 = new Intent(this, Create_action.class);
              this.startActivity(myIntent1);
              break;
          }
          else {
              Intent myIntent1 = new Intent(this, LogIn.class);
              this.startActivity(myIntent1);
              break;
          }
      case 4:
          AlertDialog dialog = onCreateDialog(DIALOG_CHOOSE_CATEGORY);
          dialog.show();
          break;
      default:
          break;
      }
  
      if (fragment != null) {
          android.app.FragmentManager fragmentManager = getFragmentManager();
          fTrans = fragmentManager.beginTransaction();
          fTrans.replace(R.id.content_frame, fragment);
          fTrans.commit();
  
          // update selected item and title, then close the drawer
          myDrawerList.setItemChecked(position, true);
          myDrawerList.setSelection(position);
          setTitle(viewsNames[position]);
          myDrawerLayout.closeDrawer(mDrawer);
      } else {
          // error in creating fragment
          Log.e("MainActivity", "Error in creating fragment");
      }
  }
  </code></pre>
  
  <p>I need to get view (LinearLayout listv) from layout of AllCategories.java. I tried using getView() with findViewById, but it returns NullPointerException. What shall I do?</p>