嘿伙计们,
我已经找了几个小时了,尝试了不同的东西,其中大部分我已经包括了。我希望你们能帮上忙,显然我没看到。
我在我的地图片段中有一个SupportMapFragment。我曾经使用过这种方法,但由于某种原因,它现在只是用一个空指针崩溃了。在搜索了几个小时并阅读了更多文档之后,我发现我使用了一种不推荐的方法,所以我尝试使用建议的方法: onMapReady。但是,即使在我使用
com.google.android.gms.maps.SupportMapFragment下面是我的带有片段的xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayoutMainAct"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<fragment
android:id="@+id/mapView"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<View
android:id="@+id/map_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>下面是以OnViewCreated开头的代码:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if(GooglePlayServicesUtil.isGooglePlayServicesAvailable(MainActivity.getContext()) == ConnectionResult.SUCCESS) {
initMap();
if (locations != null) {
addMarkers();
}
moveMapToCuracao();
}
}
public void initMap() {
final SupportMapFragment myMAPF = (SupportMapFragment) getFragmentManager()
.findFragmentById(mapView);
myMAPF.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.setPadding(50, 0, 0, 0);
mGoogleMap.getUiSettings().setMyLocationButtonEnabled(false);
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap
.setOnMyLocationChangeListener(new OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
// TODO Auto-generated method stub
GeomagneticField field = new GeomagneticField(
(float) location.getLatitude(),
(float) location.getLongitude(),
(float) location.getAltitude(), System
.currentTimeMillis());
// getDeclination returns degrees
mDeclination = field.getDeclination();
}
});
if (locations != null) {
addMarkers();
}
moveMapToCuracao();
}
});
}更新:感谢Eugen更新的代码
但是仍然会在getMapAsync()上使用NullPointerException崩溃。
发布于 2015-01-14 10:54:45
映射片段是您将其充气到另一个片段的布局的一部分(让我们称它为父片段)。膨胀的片段不是活动的子片段,而是父片段的子片段。您必须询问父片段的子片段管理器:
final SupportMapFragment myMAPF = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(mapView);https://stackoverflow.com/questions/27939453
复制相似问题