Android MapView在片段中为空
我有这个xmlAndroid MapView在片段中为空,android,dictionary,fragment,Android,Dictionary,Fragment,我有这个xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:backgrou
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#f00"
android:gravity="center">
<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
</LinearLayout>
片段加载正常,但nMap为null。祝酒词出现“谷歌地图不可用”
我找不到错误。尝试使用
getmapsync(OnMapReadyCallback回调)
而不是getMap()
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CHD=新车床(dlat,dlong);
map=((MapFragment)getFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setMyLocationEnabled(true);
addMarker(新的MarkerOptions().position(CHD.title)(“你在这里”);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(CHD,15));
animateCamera(CameraUpdateFactory.zoomTo(12),2000,空);
}
您是否生成了正确的谷歌地图API密钥
谷歌地图需要以下权限
–检查网络状态是否可以下载数据访问网络状态
–检查互联网连接状态互联网
–当谷歌地图将地图数据存储在外部存储器中时写入外部存储器WRITE_EXTERNAL_STORAGE
–使用WiFi和移动手机数据确定用户位置访问位置
–使用GPS确定用户位置访问精细位置
OpenGL ES V2–Google Maps V2所需的您是否已创建密钥并在Menifest中定义,您在哪个设备上运行此代码?似乎您正在使用emulator。@aldakur布局错误。
map\u活动
。碎片的布局是否相同?是的。名称不合适。在“活动”中的上一次y启动映射中,但现在我需要FragmentnMap=mapView.getMapAsync(this)
“te类型mapView中的方法getMapAsync(OnMapReadyCallback回调)不适用于参数myFragmentName”但是在这种情况下,我的mMap变量也是空的。在onMapReady
中设置map=mMap
,然后你可以对你的map做一些事情。我在onMapReady方法中写Log,不运行yes。活动中启动的最后一张地图
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.map_activity, container, false);
mapView = (MapView) view.findViewById(R.id.map);
mMap = mapView.getMap();
if (mMap== null)
{
Toast.makeText(getActivity().getBaseContext(),"Google Maps not Available",
Toast.LENGTH_LONG).show();
}
locReader = (LocationReader) new LocationReader(getActivity().getBaseContext(), true);
this.loc = locReader.getLastKnownLocation();
moveCamMyPosition(loc);
return view;
}
public class myFragment extends Fragment implements OnMapReadyCallback {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/* ... */
mapView.getMapAsync(this);
/* ... */
}
@Override
public void onMapReady(GoogleMap map) {
/* do something with the map */
mMap = map;
locReader = (LocationReader) new LocationReader(getActivity().getBaseContext(), true);
this.loc = locReader.getLastKnownLocation();
moveCamMyPosition(loc);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CHD = new LatLng(dlat, dlong);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
map.setMyLocationEnabled(true);
map.addMarker(new MarkerOptions().position(CHD).title("You Are Here"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(CHD, 15));
map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
}
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />