gpt4 book ai didi

java - while 循环中的 mapFrag.getMapAsync() 问题

转载 作者:行者123 更新时间:2023-12-01 18:30:21 25 4
gpt4 key购买 nike

我确实需要帮助来解决我不知道如何解决的问题。我正在尝试为我父亲申请,我将与 Map 合作。该问题具体出现在 while 循环内的 mapFrag.getMapAsync() 中。

@Override
public void onResume(){
super.onResume();
new Thread(){
public void run(){
while ( mapFrag.getMapAsync() == null){
try {
Thread.sleep(1000);
} catch (InterruptedException e){
e.printStackTrace();
}
}
runOnUiThread(new Runnable() {
@Override
public void run() {
configMap();
}
});
}
}.start();
}

while (mapFrag.getMapAsync(),此行收到错误消息'

getMapAsync(OnMapReadyCallback) in SupportMapFragment cannot be applied to ()

OnMapReadyCallback 是在 MainActivity 上实现的。

最佳答案

根据 MapFragment.getMapAsync() 的文档:

public void getMapAsync (OnMapReadyCallback callback)

Sets a callback object which will be triggered when the GoogleMap instance is ready to be used.

Note that:

This method must be called from the main thread.

The callback will be executed in the main thread.

In the case where Google Play services is not installed on the user's device, the callback will not be triggered until the user installs it. In the rare case where the GoogleMap is destroyed immediately after creation, the callback is not triggered.

The GoogleMap object provided by the callback is non-null.

特别是这个:

Parameters callback The callback object that will be triggered when the map is ready to be used.

在您的调用 mapFrag.getMapAsync() 中,没有 OnMapReadyCallback 参数。因此,您应该添加它:

...
mapFrag.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
googleMap.moveCamera(cameraUpdate);
}
})
...

并且您不应该在单独的线程中调用 getMapAsync(),因为(请参阅上面的文档链接):

  • 此方法必须从主线程调用。

  • 回调将在主线程中执行。

所以,请仔细阅读官方Get Started页。

关于java - while 循环中的 mapFrag.getMapAsync() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60179090/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com