我在Fragment中有关于MapFragment的问题。 在main_layout.xml中:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector"
android:background="@color/list_background"/>
listView是一个菜单,我用它来改变片段。 在MainActivity.java中,我通过此函数更改Fragment:
private void displayView(int _position)
{
Fragment fragment = null;
switch (_position)
{
case 0:
fragment = _YourTravelFragment;
break;
case 1:
fragment = _SettingFragment;
break;
default:
break;
}
if (fragment != null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
_DrawerList.setItemChecked(_position, true);
_DrawerList.setSelection(_position);
setTitle(_MenuTitles[_position]);
_DrawerLayout.closeDrawer(_DrawerList);
} else
{
Log.e("MainActivity", "Error in creating fragment");
}
}
我创建了两个Fragment,YourTravelFragment和SettingFragment。 在fragment_your_travel.xml中有一个MapFragement。 因此,当我第一次启动应用程序时,地图正常加载。之后,我使用滑块菜单选择&#34; SettingFragment&#34;,正常加载SettingFragment,我使用滑块菜单选择&#34; YourTravelFragment&#34;,并且应用程序有一些错误,它退出。 这是&#34; YourTravelFragment.java&#34;: `私人GoogleMap _GoogleMap; private SupportMapFragment _SupportMapFragment;
public YourTravelFragment()
{
}
@Override
public View onCreateView(LayoutInflater _inflater, ViewGroup _container, Bundle _savedInstanceState)
{
View rootView = _inflater.inflate(R.layout.fragment_your_travel, _container, false);
return rootView;
}
@Override
public void onResume()
{
super.onResume();
}
@Override
public void onPause()
{
super.onPause();
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Override
public void onLowMemory()
{
super.onLowMemory();
}`
错误:
05-12 23:42:19.432: E/AndroidRuntime(9034): FATAL EXCEPTION: main
05-12 23:42:19.432: E/AndroidRuntime(9034): android.view.InflateException: Binary XML file line #5: Error inflating class fragment
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
05-12 23:42:19.432: E/AndroidRuntime(9034): at com.khaihanhdk.journeydiary.YourTravelFragment.onCreateView(YourTravelFragment.java:37)
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.app.BackStackRecord.run(BackStackRecord.java:635)
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399)
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.os.Handler.handleCallback(Handler.java:615)
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.os.Looper.loop(Looper.java:137)
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.app.ActivityThread.main(ActivityThread.java:4960)
05-12 23:42:19.432: E/AndroidRuntime(9034): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 23:42:19.432: E/AndroidRuntime(9034): at java.lang.reflect.Method.invoke(Method.java:511)
05-12 23:42:19.432: E/AndroidRuntime(9034): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
05-12 23:42:19.432: E/AndroidRuntime(9034): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
05-12 23:42:19.432: E/AndroidRuntime(9034): at dalvik.system.NativeStart.main(Native Method)
05-12 23:42:19.432: E/AndroidRuntime(9034): Caused by: java.lang.IllegalArgumentException: Binary XML file line #5: Duplicate id 0x7f060044, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
05-12 23:42:19.432: E/AndroidRuntime(9034): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)
05-12 23:42:19.432: E/AndroidRuntime(9034): ... 18 more
这看起来像是罪魁祸首。 java.lang.IllegalArgumentException: Binary XML file line #5: Duplicate id 0x7f060044, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment