I have implemented an activity which adds MapFragment
at run time. The MapFragment
xml has static fragment
and I'm trying to get add at run time. Also I found there are some issues in Lollipop adding the map fragment at runtime. Kindly check Issue raised and temporary solution
I have also given my codes below,
fragment_map.xml
<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=".fragment.MapsFragment">
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="appCreators.bloodfinder.activity.MapsActivity"/>
<include
android:id="@+id/layout"
layout="@layout/template_custom_spinner"/>
</FrameLayout>
MapsFragment.java
Implements onMapReadyCallback
public class MapsFragment extends SupportMapFragment implements OnMapReadyCallback
In onResume
callback
@Override
public void onResume() {
super.onResume();
((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
}
this always return me null and I have also tried,
((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
this also return NullPointerException
MapsActivity.java
getSupportFragmentManager().beginTransaction().add(R.id.fragmentContainer, MapsFragment.newInstance()).commit();
I add this at onCreate
method of Activity callback.
I'm not able to figure out why I'm still getting NullPointerException
!
Some times I get Attempt to invoke interface method 'void com.google.maps.api.android.lib6.e.fl.o()' on a null object reference
Help will be appreciated!
UPDATE: Still not fixed I'm getting the following error. I looked into logs but no clue why this is happening.
Unable to resume activity {MapsActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.e.fl.o()' on a null object reference
I finally solved this issue for myself by observing the given answers in SO. I compared my code and the code in the answers. It seems to me that there is some confusion about this API and Google's implementation is very broad. I got this exception thrown repeatedly:
I still have no idea whats wrong with some of these implementations in SO questions that are getting the same exception, but this worked for me and it might work for someone else.
These are the the things you need to check:
Make sure you have both of these (I had only the api key meta-data)
You have a fragment in xml like this with the class attribute being correct as below
In my case the map fragment is a child, inside another fragment.
This parent fragment apparently should not be a MapFragment or a SupportMapFragment. After changing it from SupportMapFragment to Fragment the map showed perfectly fine.
I left everything else as it was before but instead of getting the map in onCreateView I got it in onViewCreated like this:
I changed nothing else and I did not mess with the rootView or parentView. As you would see in the the answer from @Konstantin Loginov
Try it out and let me know.
I've been struggling with similar issue a while ago. The key was to
getMap()
in a proper moment, inonViewCreated()
of theFragment
:And the XML for it looks like yours:
Then, in
onViewCreated()
you can notifyActivity
, that your map-fragment is ready to use.I hope, it helps
In my case I use raw
MapView
statically in a Fragment's xml. The problem was that I had to move the call forwardingmMapView.onCreate(...)
toonViewCreated(...)
because inonCreate()
mMapView
is not yet inflated. If you don't useMapView
then you should double-check in which moment you callgetMap()
. I must be after the internal MapView is inflated.