2

Android アプリケーションで Google Maps API v2 を使用しようとしています。次のコードを使用してプログラムでマップ フラグメントを追加し、SupportMapFragment から GoogleMap を取得しようとしましたが、マップが画面に正常に表示されていても、常に null の結果が返されます...どんな助けも大歓迎です!!!!! !!

ありがとう

public class MapActivity extends BaseFragmentActivity {


    private SchoolType mSchoolType=SchoolType.ALL;
    private GoogleMap mMap;
    private UiSettings mUiSettings;
    private SupportMapFragment mMapFragment;
    private static final String MAP_FRAGMENT_TAG = "map";
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try{
            setContentView(R.layout.map_activity);
            mMapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentByTag(MAP_FRAGMENT_TAG);
            if(mMapFragment==null)
                addMapFragment();
            setUpMapIfNeeded();

        }
        catch(Exception ex){
            System.err.println("Exception: " + ex.getMessage());

        }

    }

    private void addMapFragment(){
        try{

            GoogleMapOptions options = new GoogleMapOptions();
            options.mapType(GoogleMap.MAP_TYPE_NORMAL)
                    .zoomControlsEnabled(true) ;

            mMapFragment =  SupportMapFragment.newInstance(options);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(R.id.fragment_map_content, mMapFragment,MAP_FRAGMENT_TAG);
            //transaction.addToBackStack(null);
            transaction.commit();
        }
        catch(Exception ex){
            System.err.println("Exception: " + ex.getMessage());
        }

    }

private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap =  mMapFragment.getMap(); ***//ALWAYS RETUN NULL***
            //mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))

            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }
4

1 に答える 1

0

このように onResume() で setUpMapIfNeeded を呼び出します。

@Override
protected void onResume() {
    super.onResume();

    // In case Google Play services has since become available.
    setUpMapIfNeeded();
}
于 2013-01-19T18:55:33.573 に答える