0

Map Null point exception error. Having trouble loading the map in my fragment. Seems to always be null. I have tried loading the view in different ways but still seems to get a null map.

08-01 12:41:19.349: E/AndroidRuntime(25728): Process: com.ideamovinganddelivery, PID: 25728
08-01 12:41:19.349: E/AndroidRuntime(25728): java.lang.NullPointerException
08-01 12:41:19.349: E/AndroidRuntime(25728):    at com.idealmovinganddelivery.mapfragment.MapsFragment.setUpMapIfNeeded(MapsFragment.java:122)
08-01 12:41:19.349: E/AndroidRuntime(25728):    at com.idealmovinganddelivery.mapfragment.MapsFragment.onResume(MapsFragment.java:90)

Main Activity

   public class MainActivity extends ActionBarActivity implements
        ActionBar.TabListener,MapDialogFragment.MapDialogListener{
        @Override
    public void onDialogPositiveClick(DialogFragment dialog, String Address) {
        MapsFragment mapFrag= (MapsFragment)     getSupportFragmentManager().findFragmentById(R.id.map);
         mapFrag.addDeliveryPoint(Address); 
    }

Fragment_map XML

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_map_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.idealmovinganddelivery.MainActivity" >

     <fragment
         android:id="@+id/map"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         class="com.idealmovinganddelivery.mapfragment.MapsFragment" />

</RelativeLayout>

Map Fragment

       public class MapsFragment extends SupportMapFragment implements SearchView.OnQueryTextListener,
    OnMarkerClickListener
    {
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
            setHasOptionsMenu(true) ;


        view = inflater.inflate(R.layout.fragment_map, container,
                false);
            return view;
        }
public static void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (map == null) {
            // Try to obtain the map from the SupportMapFragment.
            map = ((SupportMapFragment) MainActivity.fragmentManager
                    .findFragmentById(R.id.map)).getMap();
            // Check if we were successful in obtaining the map.
            if (map != null)
                setUpMap();
        }

    }

1 Answers1

1

The error is correct. You cannot cast SupportMapFragment to MapsFragment here.

At the point where you are performing this cast, you are finding the view with ID R.id.map. You have defined this in your xml file to have the type SupportMapFragment here:

class="com.google.android.gms.maps.SupportMapFragment"

You should define the <Fragment> element to use your SupportMapFragment subclass, MapsFragment instead:

fragment
     android:id="@+id/map"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     class="com.idealmovinganddelivery.mapfragment.MapsFragment" />
mattgmg1990
  • 5,576
  • 4
  • 21
  • 26
  • I fixed that and now the inflate method is giving me an error inflating the fragment message at the line of the xml file I changed. – user3897880 Aug 01 '14 at 15:28
  • Seems the map throws an null point exception. I updated the error log in my original post reflecting the new error. – user3897880 Aug 01 '14 at 17:45
  • You'll need to post the code of your MapsFragment class, I can't tell what happens on line 122 where the exception occurs. – mattgmg1990 Aug 01 '14 at 18:11
  • Tried posting the code but said my post had too much code and not enough text. Line 122 is map = ((SupportMapFragment) MainActivity.fragmentManager .findFragmentById(R.id.map)).getMap(); – user3897880 Aug 01 '14 at 18:31
  • You should probably ask a new question since this seems like a new problem. But it sounds like either you have not called setContentView before line 122 executes or perhaps you are importing the wrong Fragment or FragmentManager as in [this question](http://stackoverflow.com/a/23368288/815679). Make sure that you are using Support Fragments if you are working with `SupportMapFragment`. – mattgmg1990 Aug 01 '14 at 23:15