谷歌地图V2安卓版,地图变黑

3
我在使用谷歌地图时遇到了问题。当我第一次运行应用程序时,地图正常显示,但是当我关闭并再次打开应用程序时,地图屏幕变成了黑色。 我不知道出了什么问题,但是当我清理了应用程序的所有数据后,地图又可以正常显示了。
第一次: enter image description here 第二次(当我关闭应用程序并再次执行时): enter image description here 我正在使用带有Androidx86项目的虚拟机。
你能帮助我吗?
下面是我设置地图的代码:
private void setUpMapIfNeeded() {
        if (map == null) {
            map = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        }
            if (map != null) {
                try{
                    map.setMyLocationEnabled(true);
                    map.setOnMyLocationButtonClickListener(this);
                    map.setOnMapLongClickListener(this);
                    map.setOnMarkerClickListener(this);
                    map.setOnMapClickListener(this);
                    map.setOnCameraChangeListener(this);
                    LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
                    Criteria criteria = new Criteria();
                    String provider = locationManager.getBestProvider(criteria, true);
                    Location location = locationManager.getLastKnownLocation(provider);
                    if(location!=null){
                        double latitude = location.getLatitude();
                        double longitude = location.getLongitude();
                        LatLng latLng = new LatLng(latitude, longitude);
                        //ThreadMostraImoveisJson(latLng);
                        onLocationChanged(location);
                    }           
                }
                catch (Exception ex){
                    Log.e("Erro Mapa", "Erro Setar Mapa: "+ex.getMessage());
                }
            }
    }

我的Xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapHost"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
<fragment
      android:id="@+id/map"
       android:name="com.google.android.gms.maps.SupportMapFragment"
       android:layout_width="match_parent"
       android:layout_height="match_parent" >
</fragment>
   <LinearLayout 
    android:id="@+id/layPesquisa"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#f4f4f4"
    android:orientation="horizontal"
    android:layout_gravity="right">

    <Button
        android:id="@+id/btnPesquisa"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK" />

    <EditText
        android:id="@+id/edtPesquisa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible"
         android:text="" 
     />

</LinearLayout>
</FrameLayout>

请发布你的代码。 - Maxim Shoustin
如果在活动的onPause中将map参数设置为null,能否解决您的问题? - Emil Adz
不,我有同样的问题。 - user2568768
3个回答

3

这并不是修复错误的方法,但是删除/注释掉“mapView.onPause()”对我来说解决了问题。(其他调用似乎正常工作)。 - Darek Deoniziak

1
我遇到了MapFragment显示成黑色的问题。在AndroidManifest中,我使用了以下内容:
<supports-screens
    android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="false" />

将anyDensity设置为false会使其显示为黑色。我把它设置回true,这样它对我有用。


如果我不添加<supports-screens,那么我就不需要android:anyDensity="false"。 - Jorgesys

1
在邮件讨论中发现了一个解决这个问题的简单方法。使用相对布局,将这个透明视图直接放在地图片段上方即可解决问题。例如:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

    <fragment
      android:id="@+id/map"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      class="com.google.android.gms.maps.SupportMapFragment"/>

    <View
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@android:color/transparent" />
</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接