该应用程序加载并查看显示地图的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
,所以..错误是片段yeah?我真的不知道怎么回事。任何想法为什么会引发错误? 最佳答案
我今天整天都在同一个问题上作斗争。我早点工作了。这是我的工作:更改显示的片段时,我先删除旧的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定义的片段,但这确实对我有用。希望对您有所帮助。关于java - 膨胀以前膨胀的应用程序 fragment 时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17449124/