0

有一个包含 3 个选项卡的应用程序,我已经使用选项卡主机和片段活动完成了这项工作。我在第一个选项卡中有一张地图,当我第一次启动它时它工作正常,当我切换选项卡并返回我的应用程序崩溃并给我一个错误膨胀类异常时,地图会显示。

这是我的标签更改侦听器中的代码

getActivity().getSupportFragmentManager().beginTransaction().replace(placeholder, new MapView(), "Maps").commit();

这是我的片段类中的代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.map_view, container, false);

    return view;
}

这是我的logcat

    06-28 14:53:38.575: E/AndroidRuntime(9442): FATAL EXCEPTION: main
    06-28 14:53:38.575: E/AndroidRuntime(9442): android.view.InflateException: Binary XML file line #76: Error inflating class fragment
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at com.mymapapp.app.MapView.onCreateView(MapView.java:27)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.os.Handler.handleCallback(Handler.java:587)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.os.Handler.dispatchMessage(Handler.java:92)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.os.Looper.loop(Looper.java:123)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.app.ActivityThread.main(ActivityThread.java:3691)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at java.lang.reflect.Method.invoke(Method.java:507)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at dalvik.system.NativeStart.main(Native Method)
    06-28 14:53:38.575: E/AndroidRuntime(9442): Caused by: java.lang.IllegalArgumentException: Binary XML file line #76: Duplicate id 0x7f050058, tag null, or parent id 0x0 with another fragment for com.mymapapp.custom.views.TransparentSupportMapFragment
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
    06-28 14:53:38.575: E/AndroidRuntime(9442):     ... 20 more
4

1 回答 1

1

Vidar 在这里回答了这个问题:Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

他的解决方案是:

if (view != null) {
    ViewGroup parent = (ViewGroup) view.getParent();
    if (parent != null)
        parent.removeView(view);
}
try {
    view = inflater.inflate(R.layout.map, container, false);
} catch (InflateException e) {
    /* map is already there, just return view as it is */
}
return view;
于 2013-06-28T16:41:22.117 回答