第二次打开MapFragment时出错

时间:2016-06-16 22:40:22

标签: java android xml

我使用了一个滑动菜单,我有一个地图片段,当我第一次点击位置菜单时它没有问题,但是第二次出现以下错误:

FATAL EXCEPTION: main
                                             android.view.InflateException: Binary XML file line #34: Error inflating class fragment
                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                                 at mdev.com.mymemories.fragment.LocationFragment.onCreateView(LocationFragment.java:70)
                                                 at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
                                                 at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
                                                 at android.app.BackStackRecord.run(BackStackRecord.java:635)
                                                 at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
                                                 at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
                                                 at android.os.Handler.handleCallback(Handler.java:615)
                                                 at android.os.Handler.dispatchMessage(Handler.java:92)
                                                 at android.os.Looper.loop(Looper.java:137)
                                                 at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                 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:786)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                 at dalvik.system.NativeStart.main(Native Method)
                                              Caused by: java.lang.IllegalArgumentException: Binary XML file line #34: Duplicate id 0x7f0d00be, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment

XML文件:

  <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="@drawable/bg16">
<LinearLayout
    android:layout_marginBottom="10dp"
    android:id="@+id/ln"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:id="@+id/curent_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="@string/btnposition" />

</LinearLayout>
<fragment
    android:layout_marginTop="50dp"
    android:layout_gravity="center"
    android:layout_marginRight="5dp"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.MapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
 </RelativeLayout>

MyView.class:

public class LocationFragment extends Fragment {
View rootView;
public LocationFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     rootView = inflater.inflate(R.layout.fragment2, container, false);
 }
 }

sliding menu

1 个答案:

答案 0 :(得分:0)

考虑片段管理器不喜欢使用相同的id / tag保存第二个片段:)

你知道片段管理器的用途吗?

at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)                                             
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)