错误充气类片段:重复ID,标签空,或父ID与另一个片段 [英] Error inflating class fragment: Duplicate id , tag null, or parent id with another fragment
问题描述
我试图让在其中,当用户单击项ListView控件的应用程序,在谷歌地图显示。
I am trying to make an app in which when user clicks on item in ListView, the GoogleMaps is displayed.
我曾尝试以下code:
I have tried following code:
ShowPlacesActivity.java
public class ShowPlaceActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_place);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container_showplaces, new PlaceholderFragment())
.commit();
}
getActionBar().setSubtitle(getString(R.string.activity_showplaces_subtitle));
}
}
activity_show_place.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container_showplaces"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="com.krupal.rememberthisplace.ShowPlaceActivity" tools:ignore="MergeRootFrame" />
fragment_show_place.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/listView_places"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
fragment_maps.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/mymap"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
我要显示在列表视图的项目点击地图......所以,我已经实现了两个片段如下:
I want to show maps on listview's item click...so, I have implemented two fragments as follows:
在 PlaceholderFragment.java ,我已经设置 onitemclicklistener
为:
@覆盖 公共查看onCreateView(LayoutInflater充气,容器的ViewGroup, 捆绑savedInstanceState){ 查看rootView = inflater.inflate(R.layout.fragment_show_place,集装箱,假);
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_show_place, container, false);
final MySqliteHelper db = new MySqliteHelper(getActivity().getBaseContext());
listview_places = (ListView)rootView.findViewById(R.id.listView_places);
final PlacesAdapter places_adapter = new PlacesAdapter(getActivity(),R.layout.listview_item_row,places);
listview_places.setAdapter(places_adapter);
listview_places.setLongClickable(true);
listview_places.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position,long id) {
Place place = places_adapter.getItem(position);
String dbvaluename = place.getname();
Place selectedplace = db.getPlace(dbvaluename);
dblatitude = selectedplace.getlatitude();
dblongitude = selectedplace.getlongitude();
dbplacename = place.getname();
maps = new MapsFragment();
Bundle bundle = new Bundle();
bundle.putDouble("dbplacename",dblatitude);
bundle.putDouble("dbplacename",dblongitude);
bundle.putString("dbplacename",dbplacename);
maps.setArguments(bundle);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container_showplaces,maps);
ft.addToBackStack(null);
ft.commit();
}
});
} }
和在 MapsFragment.java ,我已经展示了地图为:
and In MapsFragment.java, I have shown maps as:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_maps, container, false);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
return rootView;
}
当我点击第一时间对ListView的项目,它显示了地图......然后,当我选择其他项目,它崩溃了:
When I click first time on ListView's item, it shows the Map...Then, when I select another item, it crashes:
logcat的日志:
android.view.InflateException: Binary XML file line #7: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.krupal.rememberthisplace.MapsFragment.onCreateView(MapsFragment.java:40)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
at android.app.BackStackRecord.run(BackStackRecord.java:622)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f0b001e, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
at android.app.Activity.onCreateView(Activity.java:4248)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.krupal.rememberthisplace.MapsFragment.onCreateView(MapsFragment.java:40)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
at android.app.BackStackRecord.run(BackStackRecord.java:622)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:592)
at dalvik.system.NativeStart.main(Native Method)
编辑: <一个href="http://stackoverflow.com/questions/14083950/duplicate-id-tag-null-or-parent-id-with-another-fragment-for-com-google-androi?rq=1">this soloution没有为我工作。
this soloution didn't work for me
推荐答案
我解决它现在!
在另一种解决方案是与答案也很适合我。
The another solution is linked answer worked for me well.
<一个href="http://stackoverflow.com/a/14484640/3960528">http://stackoverflow.com/a/14484640/3960528
由于没有解释,我说 onDestroyView()
在'片段包含地图。
As explained there, I added onDestroyView()
in `fragment that contains maps.
@Override
public void onDestroyView() {
super.onDestroyView();
MapFragment f = (MapFragment) getFragmentManager()
.findFragmentById(R.id.mymap;
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
这篇关于错误充气类片段:重复ID,标签空,或父ID与另一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!