我发现很多线程都与在片段中添加地图片段有关。但他们并没有帮助我解决我的问题。我试图将一个地图片段添加到片段中。但我得到Null指针异常如下:
11-25 10:19:55.222 9101-9101/com.uma.mobile E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.uma.mobile, PID: 9101
java.lang.NullPointerException
at com.uma.mobile.subview.ShopDetails.<init>(ShopDetails.java:122)
at com.uma.mobile.activity.Shop.onCreateView(Shop.java:119)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
其中Shop.java扩展Fragment,ShopDetails扩展了RelativeLayout,其中包含mapFragement。
shop_details.xml:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/mapTile"
android:layout_marginTop="10dp"
android:padding="10dp"
android:background="@drawable/grey_gradient_rounded"
android:layout_below="@+id/description">
<RelativeLayout
android:id="@+id/mapViewContainer"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/nearestLocation"
android:layout_alignBottom="@+id/tapToView" />
<!--<fragment xmlns:android="http://schemas.android.com/apk/res/android"-->
<!--android:id="@+id/mapView"-->
<!--android:layout_width="100dp"-->
<!--android:layout_height="wrap_content"-->
<!--android:name="com.google.android.gms.maps.MapFragment"/>-->
这就是我添加地图片段的方式
mapFragment = new MapFragment();
FragmentTransaction transaction= fm.findFragmentByTag("SHOPS").getChildFragmentManager().beginTransaction();
transaction.add(R.id.mapViewContainer,mapFragment,"MAP_FRAG").commit();
这是我得到例外的地方:
try {
MapsInitializer.initialize(activity);
mapFragment.getMap().getUiSettings().setZoomControlsEnabled(false);
LatLng position = new LatLng(loc._latitude, loc._longitude);
MarkerOptions markerOptions = new MarkerOptions()
.position(position)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow_pin2x));
mapFragment.getMap().addMarker(markerOptions);
mapFragment.getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(position, 14));
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
fm.beginTransaction().hide(mapFragment).commit();
}
.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow_pin2x));
map.addMarker(markerOptions);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 14));
mapFragment.getMap()。getUiSettings()
答案 0 :(得分:4)
你应该在片段中使用mapview它也支持map v2。请参阅此https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapView
尝试一下它会起作用。
public class MyMapFragment extends Fragment {
private MapView mMapView;
private GoogleMap mMap;
private Bundle mBundle;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.map_fragment, container, false);
try {
MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
// TODO handle this situation
}
mMapView = (MapView) inflatedView.findViewById(R.id.map);
mMapView.onCreate(mBundle);
setUpMapIfNeeded(inflatedView);
return inflatedView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBundle = savedInstanceState;
}
private void setUpMapIfNeeded(View inflatedView) {
if (mMap == null) {
mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
@Override
public void onResume() {
super.onResume();
mMapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
mMapView.onDestroy();
super.onDestroy();
}
}
这是相应的res / layout / map_fragment.xml:
<?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" >
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
如果您的布局中只有一个元素,则可以省略RelativeLayout(并将xmlns声明移动到新的根元素,在本例中为com.google.android.gms.maps.MapView)