2017-06-13 9 views
0

Hallo Ich habe Viewpager mit mehr Fragmenten. auf der 3. Ansicht möchte ich Karte setzen.findFragmentById in Fragment funktioniert nicht

mein Layout mit Karten. (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> 

Ich habe diese Klasse Bildschirme in Seitenzugriffen zu setzen.

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


} 

Und auf ViewCreated App abgestürzt, da mapFragment = null. Aber ich weiß nicht, warum es null ist.

framentmanager not null

Bitte geben Sie mir Ratschläge. Ich habe versucht stundenlang mein Problem lösen, aber unsuccesfull

+0

Sie können den MapFragment anstelle des SupporrtFragment wie diese Karte = ((MapFragment) getFragmentManager() findFragmentById (R.id.map).); map.getMapAsync (dies); –

+0

Fragment-Tag wird nicht benötigt Frame-Layout ist genug – Raj

+0

Ich versuche SupportMapFragment mapFragment = ((SupportMapFragment) getFragmentManager(). FindFragmentById (R.id.map2)); aber es ist das gleiche null @ Ravindra Kushwaha – trip07

Antwort

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