title: IllegalStateException: Fragment null must be a public static class to be properly recreated from instance state
body: <p>I have the following problem to display a fragment within the map of google maps, I need to work on Android API 24 , as in previous versions if it works.
  The error is in the line of Fragment MapRestaurante in :<br>
  <code>getFmMain().getSupportFragmentManager().beginTransaction().add(R.id.activitymap, mMapFragment).commit()</code></p>
  
  <p>The same problem if I use the line:</p>
  
  <p><code>getChildFragmentManager().beginTransaction().add(R.id.activitymap, mMapFragment).commit();</code></p>
  
  <p><strong>Android Monitor</strong></p>
  
  <pre><code>&gt; FATAL EXCEPTION: main
  Process: com.jungle, PID: 4356
  java.lang.IllegalStateException: Fragment null must be a public static class to be  properly recreated from instance state.
    at     android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:435)
    at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:420)
    at com.jungle.MapRestaurant.onCreateView(MapRestaurant.java:72)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
    at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1638)
    at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
    at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
    at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:542)
    at com.jungle.MainActivity.Setup(MainActivity.java:167)
    at com.jungle.MainActivity.access$000(MainActivity.java:34)
    at com.jungle.MainActivity$1.onClick(MainActivity.java:61)
    at android.view.View.performClick(View.java:4756)
    at android.view.View$PerformClick.run(View.java:19749)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
  </code></pre>
  
  <p><strong>Clase MainActivity</strong>   </p>
  
  <pre><code>public class MainActivity extends AppCompatActivity
      implements NavigationView.OnNavigationItemSelectedListener {
  
  private ViewPager viewPager;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      viewPager = (ViewPager) super.findViewById(R.id.viewpager);
  
      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);
  
      Button btnRestaurant = (Button) findViewById(R.id.btnRestaurant);
      btnRestaurant.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
              Setup(1);
          }
      });
  }
  
  @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) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      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();
  
      if (id == R.id.nav_camera) {
          // Handle the camera action
      } else if (id == R.id.nav_gallery) {
  
      } 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;
  }
  
  
  public void maphotel(View v){
      //Intent intent = new Intent(this,MapRestaurant.class);
      //startActivity(intent);
      Setup(2);
  }
  public void mapthingsdo(View v){
      //Intent intent = new Intent(this,MapRestaurant.class);
      //startActivity(intent);
      Setup(3);
  }
  
  public FrameLayout getFrameContainer(){
     FrameLayout fm = (FrameLayout) findViewById(R.id.fragment_container);
      return fm;
   }
  private void Setup(int id){
      ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
      switch (id){
          case 1:
              if(adapter.getmFragmentTitleList().size() &gt; 0) {
                  adapter.getmFragmentTitleList().clear();
              }
  
              adapter.addFragment(new MapRestaurant(this), null);
              adapter.notifyDataSetChanged();
  
              break;
          case 2:
              if(adapter.getmFragmentTitleList().size() &gt; 0) {
                  adapter.getmFragmentTitleList().clear();
              }
              adapter.addFragment(new MapHotel(this), null);
              adapter.notifyDataSetChanged();
              break;
          case 3:
              if(adapter.getmFragmentTitleList().size() &gt; 0) {
                  adapter.getmFragmentTitleList().clear();
              }
              adapter.addFragment(new MapThingstodo(this), null);
              adapter.notifyDataSetChanged();
              break;
      }
  
       viewPager.setAdapter(adapter);
   }
  
  
  //Clase para visualizar los fragmentos y los tabs en el viewpager
  class ViewPagerAdapter extends FragmentStatePagerAdapter {
      private List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
      private  List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
      public List&lt;String&gt; getmFragmentTitleList() {
          return mFragmentTitleList;
      }
  
      public List&lt;Fragment&gt; getmFragmentList() {
          return mFragmentList;
      }
  
      public ViewPagerAdapter(FragmentManager manager) {
          super(manager);
  
      }
  
      @Override
      public Fragment getItem(int position) {
          return mFragmentList.get(position);
      }
  
      @Override
      public int getCount() {
          return mFragmentList.size();
      }
  
      public void addFragment(Fragment fragment, String title) {
  
          mFragmentList.add(fragment);
          mFragmentTitleList.add(title);
  
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
  
          return mFragmentTitleList.get(position);
      }
  
      //this is called when notifyDataSetChanged() is called
      @Override
      public int getItemPosition(Object object) {
          // refresh all fragments when data set changed
          return   POSITION_NONE;
      }
  
  
      @Override
      public void destroyItem(ViewGroup container, int position, Object object) {
          if (position &gt;= getCount()) {
              FragmentManager manager = ((Fragment) object).getFragmentManager();
              FragmentTransaction trans = manager.beginTransaction();
              trans.remove((Fragment) object);
              trans.commit();
  
          }
  
      }
  
  }
  
  }
  </code></pre>
  
  <p><strong>Fragment MapRestaturant</strong></p>
  
  <pre><code>public class MapRestaurant extends Fragment implements OnMapReadyCallback {
  
  private GoogleMap mMap;
  private SupportMapFragment mMapFragment;
  static Context contexto;
  private MainActivity fmMain;
  @SuppressLint("ValidFragment")
  public MapRestaurant(MainActivity fmMain) {
      this.fmMain = fmMain;
  }
  
  public MainActivity getFmMain() {
      return fmMain;
  }
  
  public void setFmMain(MainActivity fmMain) {
      this.fmMain = fmMain;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
  
      super.onCreate(savedInstanceState);
  }
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  View v = inflater.inflate(R.layout.activity_maps, container, false);
  mMapFragment = new SupportMapFragment() {
          @Override
          public void onActivityCreated(Bundle savedInstanceState) {
              super.onActivityCreated(savedInstanceState);
  
          }
      };
  contexto = v.getContext();
  mMapFragment.getMapAsync(this);
  //getChildFragmentManager().beginTransaction().add(R.id.activitymap, mMapFragment).commit();
   getFmMain().getSupportFragmentManager().beginTransaction().add(R.id.activitymap, mMapFragment).commit();//aqui es donde me tira el error
  
   return  v;
  }
  
  @Override
  public void onMapReady(GoogleMap googleMap) {
  mMap = googleMap;
  if (ContextCompat.checkSelfPermission(contexto, Manifest.permission.ACCESS_COARSE_LOCATION)== PackageManager.PERMISSION_GRANTED) {
          mMap.setMyLocationEnabled(true);
      }
  
      mMap.addMarker(new MarkerOptions().position(new LatLng( -25.598303, -54.575627)).title("Angelo Resto Bar Restaurant")).setIcon(BitmapDescriptorFactory.fromResource(R.drawable.restaurant));
      mMap.addMarker(new MarkerOptions().position(new LatLng(  -25.598265, -54.575681)).title("Restaurante II Frantello")).setIcon(BitmapDescriptorFactory.fromResource(R.drawable.restaurant));
      mMap.addMarker(new MarkerOptions().position(new LatLng( -25.598806, -54.575069)).title("La Tata Restaurante")).setIcon(BitmapDescriptorFactory.fromResource(R.drawable.restaurant));
      mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(-25.598806, -54.575069)));
      mMap.moveCamera(CameraUpdateFactory.zoomTo(9));
      mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
          @Override
          public void onInfoWindowClick(Marker marker) {
              NotifPopUp firstFragment = new NotifPopUp();
              fmMain.getSupportFragmentManager().beginTransaction().add(getFmMain().getFrameContainer().getId(), firstFragment).commit();
  
          }
      });
      }
  }
  </code></pre>
  
  <p>Thanks!!!</p>