当膨胀之前膨胀的片段应用程序崩溃

时间:2013-07-03 13:18:54

标签: java android fragment

应用加载并查看显示地图的EventFragment.java。当您单击另一个选项卡片段时,它会很好地加载它,当您返回到第一个选项卡(地图)时,它会崩溃并出现错误:

07-03 14:13:54.640: E/AndroidRuntime(11565): FATAL EXCEPTION: main
07-03 14:13:54.640: E/AndroidRuntime(11565): android.view.InflateException: Binary XML file line #5: Error inflating class fragment
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at co.uk.thesomewhere.townpearlfest.EventFragment.onCreateView(EventFragment.java:48)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1264)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.os.Handler.handleCallback(Handler.java:587)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.os.Looper.loop(Looper.java:143)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.app.ActivityThread.main(ActivityThread.java:4196)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at java.lang.reflect.Method.invokeNative(Native Method)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at java.lang.reflect.Method.invoke(Method.java:507)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at dalvik.system.NativeStart.main(Native Method)
07-03 14:13:54.640: E/AndroidRuntime(11565): Caused by: java.lang.IllegalArgumentException: Binary XML file line #5: Duplicate id 0x7f05000b, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
07-03 14:13:54.640: E/AndroidRuntime(11565):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
07-03 14:13:54.640: E/AndroidRuntime(11565):    ... 19 more
07-03 14:13:56.280: I/Process(11565): Sending signal. PID: 11565 SIG: 9

所以读到它说的是XML文件行5.这是XML文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/the_map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/btn_filerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingTop="10dp"
        android:drawableRight="@drawable/magglasswhite"
        android:background="@android:color/transparent"
        android:onClick="filterView" />

</FrameLayout>

恰好是<fragment所以..错误是片段是啊?我真的不知道发生了什么事。任何想法为什么会抛出错误?

1 个答案:

答案 0 :(得分:5)

我今天整天都在为同一个问题而战。我让我的工作更早。这就是我所做的:当更改显示的片段时,我首先删除旧的mapFragment,然后按照我通常在添加新片段时的操作进行操作。

Fragment oldMapFrag = getSupportFragmentManager().findFragmentById(R.id.map);
if(oldMapFrag != null) {
    getSupportFragmentManager().beginTransaction().remove(oldMapFrag).commit();
    manager.popBackStack();
    ViewGroup container = (ViewGroup)findViewById(R.id.content);
    if(container != null)
        container.removeAllViews();
}

当我想再次查看mapFragment时,我删除了之前的片段,然后再次使用mapFragment为视图充气:

Fragment currentFrag = getSupportFragmentManager().findFragmentById(R.id.content);
if(currentFrag != null) {
    getSupportFragmentManager().beginTransaction().remove(currentFrag).commit();
    manager.popBackStack();
}
View mapView = inflator.inflate(R.layout.store_finder, container, true);

我不知道为什么getSupportFragmentManager().beginTransaction().remove(oldMapFrag).commit();在第一步中有效,因为您不能删除XML中定义的片段,但这对我有用。我希望它有所帮助。