Search code examples
androidandroid-activity

cannot resolve method getMap() : AppCompatActivity


My activity class goes like this :

@SuppressWarnings("deprecation")
public class AddPostActivity extends AppCompatActivity {  

Lint shows an error like this in my code : "cannot resolve method getMap();"

The code snippet is: `

    MapFragment fm = (MapFragment)getFragmentManager() 
    .findFragmentById(R.id.maps_fragment);
    map = fm.getMap();

    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mUiSettings = map.getUiSettings();

    mUiSettings.setCompassEnabled(true); 
    mUiSettings.setMyLocationButtonEnabled(false);`

I have tried to replace getMap(); with getMapAsync(); :

        public class AddPostActivity extends FragmentActivity implements OnMapReadyCallback  {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_add_post);

            MapFragment fm = (MapFragment) getFragmentManager().findFragmentById(R.id.maps_fragment);
            fm.getMapAsync(this);
        }

        public void onMapReady(GoogleMap map) {
                map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                mUiSettings = map.getUiSettings();
                mUiSettings.setCompassEnabled(true);
                mUiSettings.setMyLocationButtonEnabled(false);
        }


    }

But appears public (of the second class) in red line

How can I add the code of the OnMapReadyCallback in my first public class, so that it appears as a duplicate public class, not giving errors?

XML

  <fragment android:id="@+id/maps_fragment" 
android:name="com.google.android.gms.maps.MapFragment" 
android:layout_width="match_parent"
android:layout_height="match_parent" />

Solution

  • Unable to find where the problem could. Let you try these lines.

    If you implement the OnMapReadyCallback you need to override the onMapReady function.

    public class GetLocationActivity extends AppCompatActivity implements OnMapReadyCallback {
    
    GoogleMap gMap;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    ....
    ...
    .
    gMap = ((MapFragment) getFragmentManager().findFragmentById(
    R.id.gmap)).getMap();
    ..
    ..
    
    }
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
     // your logic here
    }
    
    }
    

    EDITED:

    Remove these lines from your code. [ remove completely your second class]

    Then make you first class to implement the onMapReadyCallback.

    Then copy the necessary variables and logic to your first class itself.

    // Remove below lines from your code.

    public class AddPostActivity extends AddPostActivity implements OnMapReadyCallback {
    
        private UiSettings mUiSettings;
        private GoogleMap map;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.add_post_activity);
    
            MapFragment fm = (MapFragment) getFragmentManager().findFragmentById(R.id.maps_fragment);
            fm.getMapAsync(this);
        }
    
        @Override
        public void onMapReady(GoogleMap googleMap) {
    
            map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            mUiSettings = map.getUiSettings();
    
            mUiSettings.setCompassEnabled(true);
            mUiSettings.setMyLocationButtonEnabled(false);
        }
        }
    

    Instead implement the same logic in your first class itself. Let me know for queries