1

I would like my Fragment to contain both an interactable map and ListView underneath it. However I am facing difficulty of trying to incorporate both elements, as it appears that the Map dominates the fragment (though it is likely my XML code is simply wrong).

Additionally, I occassionally reach this error:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Duplicate id 0x7f04003b, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment.

My XML is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    class="com.google.android.gms.maps.SupportMapFragment" />

<ListView
    android:id="@+id/map_frag_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

And my Fragment's Code is:

public class MapFrag extends SherlockFragment {
protected ListView assocListView;
protected View mapFragView;
protected String[] assocArrayItems; 
protected ArrayAdapter<String> mapAdapter;
protected List assocList;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    if (mapFragView != null) {
        ViewGroup parent = (ViewGroup) mapFragView.getParent();
        if (parent != null)
            parent.removeView(mapFragView);
    }
    try {
        mapFragView = inflater.inflate(R.layout.maptab, null, false);
    } catch (InflateException e) {
        /* map is already there, just return view as it is */
    }


    //Creating MapFragment from SharedPreferences recently stored information 
    SharedPreferences tmpManager = MainActivity.getInstance().prefs;

    System.out.println("now within the Map Fragment...");
    String recLatitude = tmpManager.getString("recentLatitude", "default");
    String recLongitude = tmpManager.getString("recentLongitude", "default");
    String recWordAssoc = tmpManager.getString("wordAssociations", "default");

    //Most Recent Items List
    String[] theList = null; 

    //TODO: ERROR HERE !!!!!!!!ClassCastException NoSaveStateFrameLayout 
    assocListView = (ListView) mapFragView.findViewById(R.id.map_frag_view);

    mapAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,  theList);

    assocListView.setAdapter(mapAdapter);
    ((BaseAdapter) assocListView.getAdapter()).notifyDataSetChanged();

    if(mapFragView != null) { return mapFragView;}

    ((ViewGroup) assocListView.getParent()).removeView(assocListView);

    container.addView(assocListView);
    container.addView(mapFragView);

    return container;
}
}
NumenorForLife
  • 1,736
  • 8
  • 27
  • 55

1 Answers1

2

Try

android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"

on both elements.

Duplicate ID issue is related to incorrect way of using nested fragments.

From: http://developer.android.com/about/versions/android-4.2.html#NestedFragments

Note: You cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically.

See my answer here: MapFragment in Fragment, alternatives?

Community
  • 1
  • 1
MaciejGórski
  • 22,187
  • 7
  • 70
  • 94
  • Hi MaciejGorski, Unfortunately I am getting a similar error to this person (http://stackoverflow.com/questions/13837697/viewpager-with-google-maps-api-v2-mysterious-black-view) regarding having an area that ought to be populated with a Map is instead full of black. I have updated my XML with your suggestion. – NumenorForLife Jun 19 '13 at 12:22