Android多片段管理

时间:2013-07-10 14:48:35

标签: android android-fragments android-view

我在FragmentTabHost中有两个片段叫做“List”和“Map” Map调用SupportMapFragment。

我的应用程序从List开始,我切换到Map并切换回list =一切都很棒:D 但是当我切换回Map时,应用程序崩溃了,因为我复制了地图ID。

我的条件是:如果您创建了视图,则不执行任何操作。 它不会崩溃,但不会显示:/

我该怎么办?

MapFragment.java

public class MapFragment extends Fragment{

    private View view;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view != null){
            return null;
        }
        view = inflater.inflate(R.layout.map, null, false);
        return view;
    }
}

map.xml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
         class="com.google.android.gms.maps.SupportMapFragment"
         />

logcat的

07-10 16:44:36.966: E/AndroidRuntime(7693): FATAL EXCEPTION: main
07-10 16:44:36.966: E/AndroidRuntime(7693): android.view.InflateException: Binary XML file line #2: Error inflating class fragment
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at com.immo.d3androidv6.Fragment2.onCreateView(Fragment2.java:25)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1264)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.os.Handler.handleCallback(Handler.java:725)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.os.Looper.loop(Looper.java:137)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at java.lang.reflect.Method.invoke(Method.java:511)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at dalvik.system.NativeStart.main(Native Method)
07-10 16:44:36.966: E/AndroidRuntime(7693): Caused by: java.lang.IllegalArgumentException: Binary XML file line #2: Duplicate id 0x7f050007, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
07-10 16:44:36.966: E/AndroidRuntime(7693):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
07-10 16:44:36.966: E/AndroidRuntime(7693):     ... 18 more

1 个答案:

答案 0 :(得分:0)

您正在为引用SupportMapFrafment的布局进行膨胀,但您正在扩展Fragment。你可以做的只是扩展SupportMapFragment,不要覆盖任何方法或在Map选项卡上定义静态片段