I am developing a GoogleMaps v2 API app for android and have encountered a problem. I would like to use an ActionBar (via actionbarsherlock, for backwards-compatability) like in the following figure.
(source: google.com)
Without adapting my code to have the ActionBar, my maps app works wonderfully. My attempt to conform the code to use it though results in a blank screen and an actionbar. Therefore, the map itself or anything else is loaded.
Code before: (relevant parts only)
public class MapActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initiate loader, so that files can be acquired in background thread.
new Loader().execute();
}
.
.
.
}
I have not included imports or variables. Don't worry about that since the code does compile.
Code after:
public class MapActivity extends SherlockFragmentActivity {
public boolean onCreate(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0,SEARCH,0,"Search")
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
// Initiate loader, so that files can be acquired in background thread.
new Loader().execute();
return true;
}
public boolean onOptionsItemSelected (MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case SEARCH:
openSearchView();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
.
.
.
}
EDIT --
Map setup code: (Note that map
has been declared as a class variable)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Recovers map choice from previous activity.
Intent intent = getIntent();
map_descriptor = intent.getStringExtra(MAP);
MAP_CENTER = map2LatLng(map_descriptor);
map = setupMap(map);
// Initiate loader, so that files can be acquired in background thread.
new Loader().execute();
}
public GoogleMap setupMap(GoogleMap map) {
if (map == null) {
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
// Just in case null map was accidently passed to setupMap.
}
map.setMapType(3); // TERRAIN
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(MAP_CENTER)
.zoom(ZOOM_LEVEL)
.build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
map.setOnCameraChangeListener(new OnCameraChangeListener() {
public void onCameraChange(CameraPosition cameraPosition) {
ZOOM_LEVEL = cameraPosition.zoom;
recenterMap(ZOOM_LEVEL);
}
});
return map;
}
And XML layout code:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
You're not properly setting the content view for the SherlockFragmentActivity, nor are you setting up the map when it expects to be setup.
In your onCreate method, make sure you do something like this after you call super.onCreate().
setContentView(R.layout.activity_main);
map = setupMap();
Even if you're downloading information from somewhere (or whatever you are doing in that asynctask) you still need to set the content view to properly inflate the map in onCreate.
You'll also want to make sure that you call setupMap from onCreate. You can add markers at a later time.
I've been using SherlockFragmentActivity with Google Maps Android API v2 since the day that API v2 was released, and I've had no issues so it must be the order in which you are creating your view.