0

I have an issue with adding Menu in my FragmentActivity.

I have added a MapFragment and some buttons in my FragmentActivity:

enter image description here

Then I'm trying to add optionsMenu above the map but my code does not works (there is no errors during running the app).

Here is my code of FragmentActivity:

<pre>package com.bugsnguns.kartta;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Menu;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

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


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

}

DavidG
  • 24,279
  • 14
  • 89
  • 82
Anton Prokopov
  • 639
  • 6
  • 27

3 Answers3

0

You need to add a ToolBar where you can inflate your Optionsmenu. You can see how to add a toolbar (App Bar) in the Android documentation: https://developer.android.com/training/appbar/setting-up.html

Your code to inflate the menu should work just fine after adding a Toolbar

Katharina
  • 1,612
  • 15
  • 27
0

Add a ToolBar to the Activity layout

  <android.support.v7.widget.Toolbar
   android:id="@+id/my_toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:background="?attr/colorPrimary"
   android:elevation="4dp"
   />

And add in the activity

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);
Sahil Lombar
  • 145
  • 1
  • 11
  • Thanks for answer! But setSupportActionBar does not work with FragmentActivity in my code. I'm trying to change FragmentActivity to AppCompatActivity to use our code (it seems legal because AppCompatActivity extends FragmentActivity) but the app crashes in this case. – Anton Prokopov Nov 04 '16 at 16:13
0

To get the toolbar or action bar in FragmentActivity, you'll have to assign a theme to your activity which is compatible with FragmentActivity. I struggled with this myself but everything worked as soon as I assigned a Holo theme to my activity in the Manifest.

Paran Sharma
  • 1
  • 1
  • 1