0

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

3
  • 1
    Check if (mMap == null) Commented Dec 30, 2014 at 5:33
  • 1
    @MD Same guess. Need null checking Commented Dec 30, 2014 at 5:37
  • This behaviour is similar to coderanch.com/t/637421/Android/Mobile/… ...but now i am seeing this in all devices ..... googleMap==null i checked , its true so it goes inside the condition Commented Dec 30, 2014 at 5:38

3 Answers 3

1

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

    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

My Activity is Extending FragmentActivity ....this is a wiered error i am facing !
1

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

Comments

0

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... !!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.