2014-11-02 46 views
0

當我做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

回答

1

在這種情況下,你的活動佈局文件應該是這個樣子:

<?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. 
       } 
      } 
     }