Search code examples
javaandroidandroid-fragmentsandroid-mapviewosmdroid

Exception when referencing MapView in a fragment - OSMdroid


I have written an app with OSMdroid using activities, but I am now trying to port it over to fragments instead (I'm new to fragments though). I am getting the error: "java.lang.NullPointerException: Attempt to invoke virtual method 'void org.osmdroid.views.MapView.setBuiltInZoomControls(boolean)' on a null object reference"

It seems that the MapView has not yet been initialised, am I initialising in the wrong place (OnCreateView)? According to the activity lifecycle, OnCreate is called before OnCreateView, so it would make sense that it is not recognised, but I am confused as to where then to put my code.

Code for my implementation of the fragment:

 //inflating fragment layout
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_map, container, false);
        map = (MapView) view.findViewById(R.id.map);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        Configuration.getInstance().load(context, PreferenceManager.getDefaultSharedPreferences(context));

        setupMap();
    }

//initializing map
    private void setupMap() {
        //adding zoom and touch controls
        map.setBuiltInZoomControls(true);
        map.setMultiTouchControls(true);

        //getting current location using coarse/fine location so we can set centerpoint
        currentLocation = getCurrentLocation();

... code continues ...

Error stack trace:

 java.lang.NullPointerException: Attempt to invoke virtual method 'void org.osmdroid.views.MapView.setBuiltInZoomControls(boolean)' on a null object reference
                                                                               at skicompanion.skicompanion.MapFragment.setupMap(MapFragment.java:101)
                                                                               at skicompanion.skicompanion.MapFragment.onCreate(MapFragment.java:86)
                                                                               at android.app.Fragment.performCreate(Fragment.java:2214)
                                                                               at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
                                                                               at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1153)
                                                                               at android.app.BackStackRecord.run(BackStackRecord.java:800)
                                                                               at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
                                                                               at android.app.FragmentManagerImpl$1.run(FragmentManager.java:487)
                                                                               at android.os.Handler.handleCallback(Handler.java:815)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                               at android.os.Looper.loop(Looper.java:207)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5765)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

Solution

  • In your posted code it doesn't look like you need to override onCreate method in your case. just move this code:

        context = getActivity();
        Configuration.getInstance().load(context, PreferenceManager.getDefaultSharedPreferences(context));
    
        setupMap();
    

    into the onCreateView method before the return call and should be ok.