1

我希望我的 Fragment 在它下面包含一个可交互的地图和 ListView。但是,我在尝试合并这两个元素时遇到了困难,因为似乎 Map 支配了片段(尽管我的 XML 代码很可能完全是错误的)。

此外,我偶尔会遇到此错误:

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.

我的 XML 是:

<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" />

我的片段代码是:

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;
}
}
4

1 回答 1

2

尝试

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

在这两个元素上。

重复 ID 问题与使用嵌套片段的错误方式有关。

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

注意:当布局包含<fragment>. 仅当动态添加到片段时才支持嵌套片段。

在这里查看我的答案:MapFragment in Fragment, Alternatives?

于 2013-06-18T20:38:02.593 回答