I know there are much posts like this one around but there was no solution that helped me.
I got a viewpager with two fragments and on the second one is my map. But everytime I try to draw a polyline to that map I got a nullpoint exception. Only when I use setContentView it can find my map fragment (but this destroys my viewpager).
Part of my activity:
public void buttonMaps_Click(View view) {
// setContentView(R.layout.fifth_fragment);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
if (mapFragment != null) {
GoogleMap map = mapFragment.getMap();
// map1 = ((SupportMapFragment)
// getSupportFragmentManager().findFragmentById(R.id.map1)).getMap();
Polyline line = map.addPolyline(
new PolylineOptions().add(mP0, mP1, mP2, mP3, mP4, mP5, mP6, mP7, mP8).width(5).color(Color.BLUE));
}
}
My fragment xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fifth_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="312dp"
android:tag="lumpeneimer" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/map"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:onClick="buttonMaps_Click"
android:text="Button" />
</RelativeLayout>
Is there a way to draw the polyline without using setContentView?
Edit: I tried this at my fragment.java:
public class FifthFragment extends Fragment implements OnMapReadyCallback {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstaceState) {
// TODO Auto-generated method stub
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return (LinearLayout) inflater.inflate(R.layout.fifth_fragment, container, false);
@Override
public void onMapReady(GoogleMap googleMap) {
GoogleMap map = googleMap;
// TODO Auto-generated method stub
Polyline line = map.addPolyline(
new PolylineOptions().add(mP0, mP1, mP2, mP3, mP4, mP5, mP6, mP7, mP8).width(5).color(Color.BLUE));
}
but still got the NullPoint at:
mapFragment.getMapAsync(this);