Java Android 找不到符号方法 getMap()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38417589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:18:54  来源:igfitidea点击:

Android cannot find symbol method getMap()

javaandroidgoogle-mapssupportmapfragment

提问by Julia

I have a gps app that works fine on my phone. However, I see there were people having a null pointer exception in the production.

我有一个在我的手机上运行良好的 GPS 应用程序。但是,我看到有人在生产中出现空指针异常。

    SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting GoogleMap object from the fragment
    googleMap = fm.getMap();

    // Enabling MyLocation Layer of Google Map
    googleMap.setMyLocationEnabled(true);

    googleMap.setPadding(0, 0, 0, 90);

The error I'm getting from production is Caused by:

我从生产中得到的错误是由以下原因引起的:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(boolean)' on a null object reference

So I did some research and find out that getMap method has been removed and people recommend using getMapAsync(this);

所以我做了一些研究,发现 getMap 方法已被删除,人们建议使用 getMapAsync(this);

@Override
public void onMapReady(GoogleMap googleMap) {
    this.googleMap = googleMap;    //which doesn't seem to work
}

However, I'm referencing the googleMapvariable in many different methods, and I cannot move all the code to onMapReadymethod. I tried using this.googleMap = googleMapinside the onMapReadymethod, but I'm still getting null pointer exceptions whenever I try to reference the googleMapvariable. I defined the getMapAsync(this) in onCreated(), and then try to access the 'googleMap' object in onResume() and getting the null pointer exception. Is there a way to fix this? Thank you.

但是,我googleMap在许多不同的方法中引用变量,并且我无法将所有代码移动到onMapReady方法。我尝试this.googleMap = googleMaponMapReady方法内部使用,但每当我尝试引用googleMap变量时,我仍然会收到空指针异常。我在 onCreated() 中定义了 getMapAsync(this),然后尝试访问 onResume() 中的 'googleMap' 对象并获取空指针异常。有没有办法来解决这个问题?谢谢你。

回答by Harshad Pansuriya

As I suggest in thisAnswer.

正如我在这个答案中所建议的那样。

getMap()is deprecated use getMapAsync()instead of it in the following way.

getMap()不推荐使用,getMapAsync()而不是按以下方式使用。

Replace this

替换这个

googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMap();

with this

有了这个

googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMapAsync(this);

for more details visit this link

有关更多详细信息,请访问此链接

回答by Shree Krishna

Certainly you've mismatched the SupportMapFragmentand MapFragmentmeans if you have class attribute

如果你有 class 属性,你肯定会不匹配SupportMapFragmentandMapFragment意思

class ="com.google.android.gms.maps.MapFragment"

in your XMLthen you should use

在你XML然后你应该使用

MapFragment mapFragment = (MapFragment) getFragmentManager() .findFragmentById(R.id.map);

OR

或者

If you have class attr like

如果你有类属性

class="com.google.android.gms.maps.SupportMapFragment"

then you can do like as you are doing.

那么你可以像你正在做的那样做。

回答by Kintan Patel

You are getting null pointer exception because you are trying to use google map object but google map object is still null,so you need to follow this step. write this code in onCreate() method

由于您尝试使用谷歌地图对象但谷歌地图对象仍然为空,因此您收到空指针异常,因此您需要执行此步骤。在 onCreate() 方法中编写此代码

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

now impleents your class with public class Demo implements OnMapReadyCallbackin short you need to override onMapReady(Googlemap googleMap) methood. you can perform all action after callback from onMapReady().

现在实现你的课程,public class Demo implements OnMapReadyCallback简而言之你需要覆盖 onMapReady(Googlemap googleMap) 方法。您可以在从 onMapReady() 回调后执行所有操作。

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
 // Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
    }

回答by Neranja Gunarathne

You can use the getMapAsync(this)method. Please check this link for more

您可以使用该getMapAsync(this)方法。请查看此链接了解更多

回答by Prathap Reddy

Replace

代替

googleMap = ((SupportMapFragment) getSupportFragmentManager()
    .findFragmentById(R.id.googleMap)).getMap();

with

googleMap = ((SupportMapFragment) getChildFragmentManager()
    .findFragmentById(R.id.map)).getMapAsync((OnMapReadyCallback);this);