从FragmentManager获取GoogleMap时遇到问题

从FragmentManager获取GoogleMap时遇到问题

问题描述:

当我做GoogleMap map =((SupportMapFragment)manager.findFragmentById(R.id.map))。getMap()时,为什么会出现空指针异常?我必须失去了一些东西很明显:从FragmentManager获取GoogleMap时遇到问题

public class ReviewMap extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_review_map); 

     if (savedInstanceState == null) { 
      FragmentManager manager = getSupportFragmentManager(); 
      manager.beginTransaction().add(R.id.container, new MapFragment()).commit(); 

      GoogleMap map = ((SupportMapFragment) manager.findFragmentById(R.id.map)).getMap(); // Null pointer exception 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.review_map, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     return super.onOptionsItemSelected(item); 
    } 

    public static class MapFragment extends Fragment { 

     public MapFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_review_map, container, false); 
      return rootView; 
     } 
    } 
} 

我的布局:

activity_review_map.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="atorch.shortestpaths.ReviewMap" 
    tools:ignore="MergeRootFrame" /> 

fragment_review_map.xml - 这是一个我想与R.访问id.map:

<?xml version="1.0" encoding="utf-8"?> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

如果我注释掉GoogleMap map =((SupportMapFragment)manager.findFragmentById( R.id.map))。getMap()在第一行代码中,一切都按预期工作(我在ActionBar下看到一张地图);如果我离开该行,我的logcat中会出现Null指针异常,并且应用程序在显示任何地图之前崩溃。

+1

你想直接在活动中使用的地图片段,或者你想拥有自己的自定义片段含有MapView的?如果是前者,只需在您的活动的布局文件中直接使用即可。如果是后者,则需要在Fragment的布局文件中包含com.google.android.gms.maps.MapView。根据你的回答,我可以详细阐述。 – 2014-11-02 19:08:48

+0

前者:我想直接在我的活动中包含地图片段。 – Adrian 2014-11-02 19:23:26

在这种情况下,你的活动布局文件应该是这个样子:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/home_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name="com.google.android.gms.maps.SupportMapFragment"/> 
</FrameLayout> 

我裹着的FrameLayout地图片段,如果你想添加别的东西,除了在地图的活动。但FrameLayout不是必需的。

然后在你的活动中,你可以在你从onCreate()和onResume()调用的函数中设置地图。你可能并不需要所有的听众,也是我使用自定义的信息窗口的标记气球:

private GoogleMap map; 

private void setUpMyMap() { 
      if (map == null) { 
       map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
       // Check if we were successful in obtaining the map 
       if (map != null) { 
        try { 
         MapsInitializer.initialize(this); 
        } catch (Exception e) { 
         // That should not happen as we are checking for the map not being null 
        } 
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
        map.getUiSettings().setZoomControlsEnabled(false); 
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(40.38944, -98.97639), map.getMinZoomLevel() + 1)); 
        map.setOnCameraChangeListener(new OnCameraChangeListener() { 
         @Override 
         public void onCameraChange(CameraPosition position) { 
          // Do something 
         } 
        }); 
        map.setOnMapClickListener(new OnMapClickListener() { 
         @Override 
         public void onMapClick(LatLng arg0) { 
          // Do soemthing 
         } 
        }); 
        map.setOnMarkerClickListener(new OnMarkerClickListener() { 
         @Override 
         public boolean onMarkerClick(Marker marker) { 
          // A marker was clicked 
          marker.showInfoWindow(); 
          map.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()), 800, null); 
          return true; 
         } 
        }); 
        map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() { 
         @Override 
         public void onInfoWindowClick(Marker marker) { 
          // Display the detials 
         } 
        }); 
        map.setInfoWindowAdapter(new CustomInfoWindowAdapter(this)); 
       } else { 
        // This device probably does not support Google Play Services. 
       } 
      } 
     }