user4388663
user4388663

Reputation:

Null pointer Exception for findFragmentById finding map view

What is happening:: I am not able to find the map view that returns this line fm.findFragmentById(R.id.mySupportMapId) as null

CODE

in onResume:

FragmentManager fm=getActivity().getSupportFragmentManager();
SupportMapFragment smf = (SupportMapFragment) fm.findFragmentById(R.id.mySupportMapId);
googleMap=smf.getMap();

in xml

 <FrameLayout
                android:id="@+id/rootJobsLocationId"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical" >

                <fragment
                    android:id="@+id/mySupportMapId"
                    android:name="com.google.android.gms.maps.SupportMapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="center" />

other code

Error

smf becomes null

note: i have a device that has google play services.... this error was not there when i was using regularfragment instead of support fragment

Upvotes: 0

Views: 667

Answers (3)

NAP-Developer
NAP-Developer

Reputation: 3796

Try this code on your java file,

First of all YourActivity is Extending FragmentActivity or ActionBarActivity

put this code on Fragment java file onCreateView() method

    SupportMapFragment  googleMap = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapview);

And also to destroy mapview on fragment,

@Override
public void onDestroyView() {
    super.onDestroyView();
    SupportMapFragment suMapFrag = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.mapview);
    if (suMapFrag != null)
        getSupportFragmentManager().beginTransaction().remove(suMapFrag ).commit();
}

I hope this code working fine... !!!

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Use this

    (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mySupportMapId)).getMap();
SupportMapFragment smf = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
googleMap = smf.getMap();

Instead of

    FragmentManager fm=getActivity().getSupportFragmentManager();
SupportMapFragment smf = (SupportMapFragment) fm.findFragmentById(R.id.mySupportMapId);
googleMap=smf.getMap();

Upvotes: 1

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Try this way

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/iqamah_map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.SupportMapFragment" />

// Java code

    public class xxxx extends FragmentActivity {

   GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xxx);
map =((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.iqamah_map)).getMap();

    }
}

Upvotes: 1

Related Questions