找不到符号方法getMap()

时间:2017-02-07 12:07:20

标签: android

private GoogleMap mMap;

我从MainActivity开始一项活动:

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.nav_car) {
        Intent detailIntent = new Intent(this, Map_Activity.class);
        startActivity(detailIntent);
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

并且所有工作正常,直到我得到(错误:(222,97)错误:在这里找不到符号方法getMap():

private void setUpMap() {
    if (mMap == null) {
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        mMap.setMyLocationEnabled(true);
        mMap.setOnMyLocationChangeListener(this);

    }
}

请帮我解决这个问题!

3 个答案:

答案 0 :(得分:4)

使用getMapAsync()

SupportMapFragment
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap  = googleMap;
        }
    });

您可以找到文档here

答案 1 :(得分:0)

Google地图的实施现已更改。您需要使用getMapAsync(OnMapReadyCallback回调)

这个新的tutorial将帮助您入门。

答案 2 :(得分:0)

>

private void setUpMapIfNeeded() { if (mMap == null) { SupportMapFragment mapFrag = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)); mapFrag.getMapAsync(this); if (mMap != null) { setUpMap(); } } }