android - 找不到符号方法 getMap()

标签 android

private GoogleMap mMap;

我从 MainActivity 开始一个类似的 Activity :

@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;
}

一切正常,直到我得到 ( Error:(222, 97) error: Cannot find symbol method getMap() here:

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);

    }
}

请帮我解决这个问题!

最佳答案

使用SupportMapFragmentgetMapAsync()

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

您可以找到文档here

关于android - 找不到符号方法 getMap(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42089495/

相关文章:

android - 使用 React-Native 监听 Android 上的传入链接

android - 尝试连接到Web服务器时应用崩溃

android - 如何一次性向sqlite数据库中插入数据

安卓 : Hide RelativeLayout with animationwhen scroll down/up listview

android - android :softinputmode ="adjustpan" enabled时scrollview不会滚动

java - Android 设备之间的 wifi 文件传输速度更快?

android - 动态设置 View 的id

android - fastboot 和 adb 不能与 sudo 一起使用

android - Proguard 与 Roboguice

android - 来自 Drawable 的 AndEngine 纹理