Question

I've made an XML layout for a ListView header and I also made a class that extends RelativeLayout to manipulate its elements. One of the elements is a GoogleMap fragment.

The class initialization is basically:

public class HeaderView extends RelativeLayout {
    public HeaderView(final Context context, final MyData data) {
        super(context);

        // Line #8 - inflation error
        View.inflate(context, R.layout.view_header, this);

        // Get view elements, set text, image, etc.

        // Can't use getSupportFragmentManager() because class is not FragmentActivity
        // How can I get the map then?
        GoogleMap googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    }
}

And the context being passed is an activity's base context (this.getBaseContext()). Instantiating this class results in the error:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.view.InflateException: Binary XML file line #8: Error inflating class fragment

I've already added the required meta-data to the manifest file, even because I use another map in a different class (which btw extends FragmentActivity, so no trouble with map here).

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="@string/google_api_key"/>

So basically, it's a 2 in 1 question, how can I inflate the view properly and how to get a reference to the map fragment?

This is the XML:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white">

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="65dp"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:id="@+id/map"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <com.android.volley.toolbox.NetworkImageView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:id="@+id/seller_image"
        android:scaleType="fitCenter"
        android:layout_below="@+id/map"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp" />

</RelativeLayout>
Was it helpful?

Solution

Have you tried passing the Activity as a parameter to the constructor and calling getSupportFragmentManager() on that?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top