0

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);
Dennis
  • 90
  • 12
  • 4
    The solution to a `NullPointerException` is allways the same, find the value which is null and initialize it or don´t try to acces variables or methods on it. – SomeJavaGuy Nov 03 '15 at 14:39
  • you are getting this error because of not initialize the polyline. – RAAAAM Nov 03 '15 at 14:42
  • Error is because "mapFragment" is null. It doesnt get the mapfragment – Dennis Nov 03 '15 at 14:43
  • @KevinEsche I know which value and if i use setContentView it works but this destroys my Viewpager. – Dennis Nov 03 '15 at 14:44

1 Answers1

4

maps can't take a while before is correctly initialized by the system. You should let your Activity/Fragment implement OnMapReadyCallback, registering this as callback

mapFragment.getMapAsync(this);

and wait until

@Override
public void onMapReady(GoogleMap googleMap) {

is invoked to initilaize the GoogleMap's object.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305