我希望我的 Fragment 在其下方包含可交互的 map 和 ListView。但是,我在尝试合并这两个元素时遇到困难,因为 Map 似乎主导了 fragment (尽管我的 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" />
我的 fragment 代码是:
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;
}
}
最佳答案
尝试
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
在两个元素上。
重复 ID 问题与使用嵌套 fragment 的不正确方式有关。
发件人: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.
在这里查看我的回答:MapFragment in Fragment, alternatives?
关于android - 在同一 fragment 中合并 Map 和 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17176513/