I have a map offline mapBox, but the map does not load because it is null. I followed the directions of the site: https://www.mapbox.com/android-sdk/
In layout
<com.mapbox.mapboxsdk.views.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
mapbox:access_token="@string/access_token"
/>
In Fragment:
import com.mapbox.mapboxsdk.MapFragment;
........
public class ConctactsFragment extends MapFragment {
.........
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getActivity();
mRootView = inflater.inflate(R.layout.fragment_map, container, false);
mMapView =(MapView)mRootView.findViewById(R.id.mapView);
XmlPullParser parser=getResources().getXml(R.xml.attrs);
AttributeSet attrs = Xml.asAttributeSet(parser);
mMapView = new MapView(mContext, attrs);
Log.d(TAG, "The mapView in onCreateView: " + this.getMap());
//The mapView in onCreateView: null
mMapView.setStyleUrl(Style.MAPBOX_STREETS);
mMapView.setCenterCoordinate(new LatLng(40.956645, 14.304816));
mMapView.setZoomLevel(11);
return mRootView;
}
@Override
public void onStart() {
Log.d(TAG, "The mapView in OnStart: " +this.getMap());
// The mapView in OnStart: null
super.onStart();
mMapView.onStart();
}
}
The error is:
java.lang.NullPointerException
at com.mapbox.mapboxsdk.MapFragment.onStart(MapFragment.java:70)
onStart(...) mMapView=null
better to do inonResume();
mMapView.onStart();
– M D