问题
I found many threads related to adding a map fragment within a fragment. But they didn't help me solve my problem. Im trying to add a map fragment to a fragment. But Im getting Null pointer exception as below :
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)
where Shop.java extends Fragment and ShopDetails extends RelativeLayout which has mapFragement in it.
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"/>-->
This is how I'm adding the map fragment
mapFragment = new MapFragment();
FragmentTransaction transaction= fm.findFragmentByTag("SHOPS").getChildFragmentManager().beginTransaction();
transaction.add(R.id.mapViewContainer,mapFragment,"MAP_FRAG").commit();
This is the place where I get the exception:
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));
in mapFragment.getMap().getUiSettings()
回答1:
you should use mapview in fragment it also support map v2. see this https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/MapView
try this it will work.
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();
}
}
And here is the corresponding 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>
You can omit the RelativeLayout (and move the xmlns declartion up to your new root element, in this case the com.google.android.gms.maps.MapView) if you have just one element in your layout like in this example.
来源:https://stackoverflow.com/questions/20198750/android-mapfragment-within-a-fragment