我在android中有一个活动,应该使用谷歌地图api v2显示谷歌地图,顶部有一个菜单。地图看起来应该是这样,但菜单从来没有出现过某种原因。
我已经调试了应用程序,并且永远不会调用onCreateOptionsMenu(Menu menu)
方法。这是为什么?
这是我的活动:
public class MapsActivity extends Activity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_maps, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.menu_item_direction) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* call {@link #setUpMap()} once when {@link #mMap} is not null.
* <p/>
* If it isn't installed {@link SupportMapFragment} (and
* {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
* install/update the Google Play services APK on their device.
* <p/>
* A user can return to this FragmentActivity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not
* have been completely destroyed during this process (it is likely that it would only be
* stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
* method in {@link #onResume()} to guarantee that it will be called.
*/
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 = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p/>
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
这是我的地图布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="tp1.inf8405.itinerary.activities.MapsActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"/>
</RelativeLayout>
最后,这是res / menu中的菜单布局:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_item_direction"
android:title="@string/action_menu_directions"/>
</menu>
答案 0 :(得分:5)
解决方案是让我的活动类继承自ActionBarActivity
答案 1 :(得分:1)
我认为您可以按照以下方式更改AppTheme
:
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
它会显示其中包含ActionBar
的{{1}}。
此外,您可以从我的Github here中引用示例源代码。
答案 2 :(得分:0)
从AppCompatActivity扩展而不是ActionBarActivity
答案 3 :(得分:0)
公共类AddressSelectorActivity扩展**Activity**
公共类AddressSelectorActivity扩展 AppCompatActivity
表示使用AppCompatActivity&#39;
答案 4 :(得分:-1)
在onCreateOptionsMenu()
中,返回true
,而不是调用超级方法。
According to the doc,您需要返回true
才能显示菜单。