Instantiating SupportMapFragment accidentally throws NullPointerException
I have it SupportMapFragment
embedded in another snippet. I am getting NPE from this line (note that I cannot find the reason for this as it now happens by accident):
mMap = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapview)).getMap();
( getMap()
is what exactly returns null)
However, when I change it as explained in this answer so that:
mMap = ((SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.mapview)).getMap();
then I also get NPE.
I am working with support library v4: 19.1.0.
The problem may be related to a recent project migration from Eclipse (no Gradle) to Android Studio.
source to share
Make sure you are Activity
dynamically inflating it Fragment
.
Note. You cannot inflate a layout into a fragment if that layout includes a. Nested fragments are only supported when added to a fragment dynamically.
For example:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment);
if(fragment == null){
MainActivityFragment mMainActivityFragment = new MainActivityFragment();
fm.beginTransaction()
.replace(R.id.fragment, mMainActivityFragment, "MainActivityFragment")
.commit();
}
}
}
R.layout.activity_main
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
MainActivityFragment
public class MainActivityFragment extends Fragment {
private SupportMapFragment supportMapFragment;
private GoogleMap mGoogleMap = null;
public MainActivityFragment() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
supportMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.container_map);
if (supportMapFragment == null) {
supportMapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.container_map, supportMapFragment).commit();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mRoot = inflater.inflate(R.layout.fragment_main, container, false);
return mRoot;
}
@Override
public void onResume() {
createMapView();
super.onResume();
}
private void createMapView() {
mGoogleMap = supportMapFragment.getMap();
if (mGoogleMap != null) {
mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
return false;
}
});
}
}
}
R.layout.fragment_main
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
source to share