0

我在 FragmentTabHost 中有两个 Fragment,名为“List”,“Map”Map 调用 SupportMapFragment。

我的应用程序从列表开始,我切换到地图并切换回列表 = 一切都很好:D 但是当我切换回地图时,应用程序崩溃,因为我复制了地图 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;
    }
}

地图.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"
         />

日志猫

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
4

1 回答 1

0

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

于 2013-07-10T15:00:43.373 回答