title: Initialize a fragment in android before showing it's content
body: <p>I have a simple android app I did mostly for practice. It contains MainActivity and 2 fragments, one with ImageView and second one with Listview. I tried to fill ListView using ArrayAdapter, but currently I'm stuck this error:</p>
  
  <pre><code>FATAL EXCEPTION: main
  Process: com.ikurek.pwr, PID: 2792
  java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
      at com.ikurek.pwr.Budynki.onCreateView(Budynki.java:40)
      at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5417)
  </code></pre>
  
  <p>If I understand correctly, It's caused by my app trying to fill the ListView before the view is even created. I can't figure out how to create the view before my filling function fires. How can I do it?</p>
  
  <p>My MainActivity code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity
      implements NavigationView.OnNavigationItemSelectedListener {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                      .setAction("Action", null).show();
          }
      });
  
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
              this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
      drawer.setDrawerListener(toggle);
      toggle.syncState();
  
      NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
      navigationView.setNavigationItemSelectedListener(this);
  
  
  }
  
  @Override
  public void onBackPressed() {
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      if (drawer.isDrawerOpen(GravityCompat.START)) {
          drawer.closeDrawer(GravityCompat.START);
      } else {
          super.onBackPressed();
      }
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      int id = item.getItemId();
  
  
      if (id == R.id.action_settings) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  
  @SuppressWarnings("StatementWithEmptyBody")
  
  @Override
  public boolean onNavigationItemSelected(MenuItem item) {
      // Handle navigation view item clicks here.
      int id = item.getItemId();
      FragmentManager fragmentManager = getSupportFragmentManager();
  
      //Obsługa poszczególych opcji menu
      if (id == R.id.nav_Budynki) {
  
          Budynki fragment = null;
          fragment = new Budynki().newInstance();
          fragmentManager.beginTransaction().replace(R.id.startScreen, fragment).commit();
  
      } else if (id == R.id.nav_Mapa) {
  
          Mapa fragment = null;
          fragment = new Mapa().newInstance();
          fragmentManager.beginTransaction().replace(R.id.startScreen, fragment).commit();
  
      } else if (id == R.id.nav_slideshow) {
  
      } else if (id == R.id.nav_manage) {
  
      } else if (id == R.id.nav_share) {
  
      } else if (id == R.id.nav_send) {
  
      }
  
  
  
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      drawer.closeDrawer(GravityCompat.START);
      return true;
  }
  </code></pre>
  
  <p>And My ListView fragment code:</p>
  
  <pre><code>public class Budynki extends Fragment {
  
  public Budynki() {
  }
  
  
  public static Budynki newInstance() {
      Budynki fragment = new Budynki();
      return fragment;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      return inflater.inflate(R.layout.fragment_budynki, container, false);
  }
  
  public void makeListViewArray()
  {
      String[] arrayBudynki ={"A","B","C"};
      ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter &lt;String&gt; (getActivity(), android.R.layout.simple_list_item_1, arrayBudynki);
      ListView lista= (ListView) getView().findViewById(R.id.listView);
      lista.setAdapter(adapter);
  }
  </code></pre>
  
  <p>I tried to find the solution myself, but I'm afraid I can't. I tried to call makeListViewArray() in both OnCreate() and OnCreateView(), without any success. Help please :-D</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I changed my fragment to :</p>
  
  <pre><code>@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_budynki, container, false);
      // make changes to view and its children here
      String[] arrayBudynki ={"A","B","C"};
      ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter &lt;String&gt; (getActivity(), android.R.layout.simple_list_item_1, arrayBudynki);
      ListView lista= (ListView) getView().findViewById(R.id.listView);
      lista.setAdapter(adapter);
      return view;
  }
  </code></pre>
  
  <p>And I still get the same error, ideas please?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Budynki.xml fragment:</p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context="com.ikurek.pwr.Budynki"&gt;
  
  &lt;ListView
      android:layout_width="wrap_content"
      android:layout_height="496dp"
      android:id="@+id/listView"
      android:layout_gravity="center_horizontal|top" /&gt;
  </code></pre>
  
  <p></p>