-1

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>
tbodt
  • 16,609
  • 6
  • 58
  • 83
Xjasz
  • 1,238
  • 1
  • 9
  • 21
  • worthless help. A year ago when I read the documentation they didn't explicitly mention this. You would know this if you read the documentation. – Xjasz Feb 03 '15 at 21:00
  • I already had the minSdkVersion to 11 thought that was high enough to use the methods I needed. Thanks for the support tip reading documentation now climb back into your hole kid. :0 – Xjasz Feb 03 '15 at 21:05
  • 1
    http://stackoverflow.com/questions/28096851/mapview-google-maps-nullpointerexception/28097919#28097919 Guess this would help. – SlashG Feb 03 '15 at 21:11
  • API 11 needs supportmapfragment did it thanks tbodt – Xjasz Feb 03 '15 at 22:33
  • 1
    I'm wondering how many times people will ask this kind of questions - trying to nest fragment via xml... – Marian Paździoch Feb 04 '15 at 07:01

1 Answers1

0

This is the answer:

Note: You cannot inflate a layout into a fragment when that layout includes a fragment. Nested fragments are only supported when added to a fragment dynamically.

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103