我希望我的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;
}
}
答案 0 :(得分: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?