Fragment中的findFragmentById不起作用

时间:2017-06-13 11:01:50

标签: java android android-fragments android-viewpager

你好我有更多片段的viewpager。在第3个视图我想放地图。

我的地图布局。 (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>

我有这个课程在网页浏览中设置屏幕。

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));
    }


}

在ViewCreated应用程序崩溃,因为mapFragment = null。但我不知道为什么它是空的。

framentmanager not null

请给我建议。我试了好几个小时来解决我的问题但是没有问题

1 个答案:

答案 0 :(得分:5)

     @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);
  }