2
votes

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)
1
but in onStart(...) mMapView=null better to do in onResume(); mMapView.onStart();M D
It is called onStart() before. The crash of app is in onStart()Adriana Carelli
How is getMap()-method defined? Is it set?Christopher
No, it is not set, it is a standard method of MapFragmentAdriana Carelli

1 Answers

4
votes

Why are you extending MapFragment and defining your own layout? Anyway this looks like there is no R.id.mapView in your R.layout.fragment_map. Can't really say more without the full layouts and fragment loading logic.

Here is some code that works (using mapbox SDK 2.2, Android SDK 23):

MainActivity class:

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onStart() {
        super.onStart();

        // Load the fragment dynamically
        getFragmentManager()
                .beginTransaction()
                .add(R.id.fragment_container_layout, MyMapFragment.create())
                .commit();
    }

}

Custom Fragment:

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.views.MapView;

public class MyMapFragment extends Fragment {
    private MapView mMapView;

    public static MyMapFragment create() {
        return new MyMapFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.my_map_fragment, container, false);

        mMapView = (MapView) rootView.findViewById(R.id.mb_map_view);

        mMapView.setStyleUrl(Style.MAPBOX_STREETS);
        mMapView.setCenterCoordinate(new LatLng(40.956645, 14.304816));
        mMapView.setZoomLevel(11);
        mMapView.onCreate(savedInstanceState); // If this line is omitted there is a native crash in the MapBox library

        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();
        mMapView.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    public void onPause()  {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        mMapView.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }
}

activity_main.xml (layout for main activity):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/fragment_container_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

my_map_fragment.xml (layout for custom map fragment):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:custom="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <com.mapbox.mapboxsdk.views.MapView
        android:id="@+id/mb_map_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        custom:access_token="@string/mb_token"/>
</FrameLayout>