Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findFragmentById in Fragment not work

Hello I have viewpager with more fragments. on 3rd view I want put map.

my Layout with maps. (fragment_poloha ↓)

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/map2"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="paradox.galopshop.InfoItem" />
</FrameLayout>

I have this class to set screens in pageviews.

public class DemoFragment extends Fragment implements OnMapReadyCallback {


    public static FragmentManager frag;
    LayoutInflater inflater;

    @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
      int position = FragmentPagerItem.getPosition(getArguments());

    switch (position){
        case 0: return inflater.inflate(R.layout.fragment_strucne, container, false);
        case 1: return inflater.inflate(R.layout.fragment_opis, container, false);
        case 2: return inflater.inflate(R.layout.fragment_poloha, container, false);
        case 3: return inflater.inflate(R.layout.fragment_demo2, container, false);
    }
    return inflater.inflate(R.layout.fragment_strucne, container, false);
  }

  @Override
  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    int position = FragmentPagerItem.getPosition(getArguments());

      switch (position){
          case 0: break;
          case 1: break;
          case 2:
              // Get the SupportMapFragment and request notification
              // when the map is ready to be used.
              FragmentManager fm = getActivity().getSupportFragmentManager();
              SupportMapFragment mapFragment = ((SupportMapFragment) fm.findFragmentById(R.id.map2));

           //   SupportMapFragment mapFragment = (SupportMapFragment) view.findViewById(R.id.map);
              mapFragment.getMapAsync(this);

              break;
          case 3: break;
      }
    //TextView title = (TextView) view.findViewById(R.id.item_title);
    //title.setText(String.valueOf(position*3));
  }


    /**
     * Manipulates the map when it's available.
     * The API invokes this callback when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user receives a prompt to install
     * Play services inside the SupportMapFragment. The API invokes this method after the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        // Add a marker in Sydney, Australia,
        // and move the map's camera to the same location.
        LatLng sydney = new LatLng(48.3061, 18.0764);
        googleMap.addMarker(new MarkerOptions().position(sydney)
                .title("Marker in Nitra"));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sydney, 15.0f));
    }


}

And on ViewCreated app crashed because mapFragment=null. But I don't know why it's null.

framentmanager not null

Please give me advice. I Tryed for hours solve my problem but unsuccesfull

like image 233
trip07 Avatar asked Dec 19 '22 06:12

trip07


1 Answers

     @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
      int position = FragmentPagerItem.getPosition(getArguments());

    switch (position){
        case 0: return inflater.inflate(R.layout.fragment_strucne, container, false);
        case 1: return inflater.inflate(R.layout.fragment_opis, container, false);
        case 2:     View rootView =inflater.inflate(R.layout.fragment_poloha, container, false);
                    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map2);
                    mapFragment.getMapAsync(this);
                return rootView;
        case 3: return inflater.inflate(R.layout.fragment_kontakt, container, false);
    }
    return inflater.inflate(R.layout.fragment_strucne, container, false);
  }
like image 72
trip07 Avatar answered Dec 27 '22 09:12

trip07