我的片段显示白屏而不是谷歌地图,为什么?

时间:2016-02-11 00:04:42

标签: java android google-maps android-fragments android-viewpager

我必须在主要活动中为PagerView设置地图片段。这是片段的代码。

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.doctorfinderapp.doctorfinder.R;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class DoctorMapsFragment extends Fragment implements OnMapReadyCallback {

private GoogleMap googleMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.doctor_maps_fragment, container, false);
    SupportMapFragment mapFragment = SupportMapFragment.newInstance();
    getChildFragmentManager().beginTransaction().add(R.id.map,mapFragment);
    mapFragment.getMapAsync(this);
    return view;

}

@Override
public void onMapReady(GoogleMap gMap) {
    googleMap = gMap;
    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

@Override
public void onResume() {
    super.onResume();
}

@Override
public void onPause() {
    super.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}
}

这是doctor_maps_fragment.xml

<?xml version="1.0" encoding="utf-8"?>  
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map" />

我该如何解决?当我留在地图上时,布局显示白屏,我不知道是什么问题。

2 个答案:

答案 0 :(得分:0)

多件事,你的布局错了,因为你正在使用&#39; MapView&#39;没有&#39; android:name&#39;属性。

如果您想使用MapView而不是SupprtMapFragment,请将您的布局更改为:

<com.google.android.gms.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

此外,您必须调用mapViews onCreate函数,否则不会显示任何内容,因此请将onCreateView更改为:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.doctor_maps_fragment, container, false);
    mMapView = (MapView) view.findViewById(R.id.map);
    mMapView.onCreate(null);
    mMapView.getMapAsync(this);
    return view;
}

答案 1 :(得分:0)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.doctor_maps_fragment, container, false);
    SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
    getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
    mMapFragment.getMapAsync(this);
    return view;

}

xml文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/map"/>