Question

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();
        }

    }
Was it helpful?

Solution

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" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top