I'm getting NullPointerException
on certain devices
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.MapFragment.getMap()' on a null object reference
at nick.Fragment_Maps_Main.onActivityCreated(Fragment_Maps_Main.java:376)
What I've seen on Stack Overflow is the most people use it on the onActivityCreated()
and anywhere after that. Which is what I am doing in the code below.
It's working on 90 out of 100 devices. On 10 devices it never works. These 10 devices are different Android versions too all over API 11.
Here is my code.
public class MapClass extends Fragment {
private static ViewGroup mapsView;
private static final String TAG = "MapsFragment";
private MapFragment MAPFRAG;
private GoogleMap GMAP;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
setRetainInstance(true);
mapsView = (ViewGroup) inflater.inflate(R.layout.fragment_maps, container, false);
return mapsView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
MAPFRAG = (MapFragment) getFragmentManager().findFragmentById(R.id.mapviewmain);
GMAP = MAPFRAG.getMap(); // --------------->THIS IS LINE 376
}
}
Here is my XML.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RL_MAPS_OVERALLSCREEN"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true" >
<fragment
android:id="@+id/mapviewmain"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="gov.in.dnr.Fragment_Maps_TouchSupport" />
</RelativeLayout>