Reputation: 7524
I tried to implement Google Maps in my App. I use a Navigation Drawer and call my Fragment like this:
//Main-Class
public void onNavigationDrawerItemSelected(int position) {
Fragment myFramgent = null;
switch (position) {
...
case 2 :
myFramgent = new Fillilaen();
System.out.println("FILLIAEN");
break;
...
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, myFramgent)
.commit();
}
The Fillialaen Class
public class Fillilaen extends SupportMapFragment implements ... {
private GoogleMap mMap;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.fillialen2, container, false);
return myView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SupportMapFragment fragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.location_map);
fragment.getMapAsync(this); //ERROR COMES HERE
}
public void onMapReady(GoogleMap googleMap) {
//never enter this..
mMap = googleMap;
Marker hamburg = mMap.addMarker(new MarkerOptions().position(HAMBURG).title("Hamburg"));
}
}
My XML
<fragment
android:id="@+id/location_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
I got a NullPointerException and I absolutely don't know why. I googled a lot and find similar problems, but nothing helped.
The begin of the Nullpointer Exception:
FATAL EXCEPTION: main Process: roth.com.test6, PID: 13097 java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.maps.api.android.lib6.e.fl.o()' on a null object reference at com.google.maps.api.android.lib6.e.y.b(Unknown Source) ....
THANK YOU!
Upvotes: 1
Views: 186
Reputation: 43322
You're trying to put a SupportMapFragment nested inside an outer SupportMapFragment.
If your Fragment extends SupportMapFragment, there is no need to inflate any layout xml, and you can just call this.getMapAsync()
since this
refers to a SupportMapFragment:
public class MyMapFragment extends SupportMapFragment
implements OnMapReadyCallback {
private GoogleMap mMap;
private Marker marker;
public MyMapFragment () {
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
//"this" is a SupportMapFragment,
//and "this" implements OnMapReadyCallback
// so you can call this.getMapAsync(this) here:
getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
setUpMap();
}
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
//remove previously placed Marker
if (marker != null) {
marker.remove();
}
//place marker where user just clicked
marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
});
}
}
Upvotes: 2