首页 > 代码库 > 用 FragmentManager 替换时使用 GoogleMaps 崩溃 app
用 FragmentManager 替换时使用 GoogleMaps 崩溃 app
我要用下面的代码来加载的片段的项已被选定在 NavigationDrawer,然而 MapFragment 包含它里面的谷歌地图片段和它崩溃后试图打开它第二次。
这里是我使用的代码:
@Override public void onNavigationDrawerItemSelected(int position) { // update the main content by replacing fragments FragmentManager fragmentManager = getSupportFragmentManager(); if (position == 0) { fragmentManager.beginTransaction() .replace(R.id.container, MapFragment.newInstance("a", "b")) .commit(); } else { fragmentManager.beginTransaction() .replace(R.id.container, PlaceholderFragment.newInstance(position + 1)) .commit(); } }
在这里是的日志:
03-01 14:18:25.870 27175-27175/com.br.appname E/﹕ Device driver API match Device driver API version: 23 User space API version: 2303-01 14:18:25.870 27175-27175/com.br.appname E/﹕ mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct 30 09:36:10 KST 201303-01 14:19:12.655 27175-27175/com.br.appname E/AndroidRuntime﹕ FATAL EXCEPTION: main android.view.InflateException: Binary XML file line #13: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719) at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) at android.view.LayoutInflater.inflate(LayoutInflater.java:498) at android.view.LayoutInflater.inflate(LayoutInflater.java:398) at com.br.appname.fragments.MapFragment.onCreateView(MapFragment.java:74) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.IllegalArgumentException: Binary XML file line #13: Duplicate id 0x7f070055, tag null, or parent id 0x7f070054 with another fragment for com.google.android.gms.maps.MapFragment at android.app.Activity.onCreateView(Activity.java:4971) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695) at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) at android.view.LayoutInflater.inflate(LayoutInflater.java:498) at android.view.LayoutInflater.inflate(LayoutInflater.java:398) at com.br.appname.fragments.MapFragment.onCreateView(MapFragment.java:74) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440) at android.os.Handler.handleCallback(Handler.java:730) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5419) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025) at dalvik.system.NativeStart.main(Native Method)
仅以 MapFragment 发生这种情况。PlaceholderFragment 只是正常。
更新
fragment_map.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.br.remotepark.fragments.MapFragment"> <com.sothree.slidinguppanel.SlidingUpPanelLayout android:id="@+id/sliding_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom"> <fragment android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.google.android.gms.maps.MapFragment" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#eee" android:orientation="vertical" android:clickable="true" android:focusable="false"> <LinearLayout android:layout_width="match_parent" android:layout_height="68dp" android:orientation="horizontal"> <TextView android:id="@+id/name" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textSize="14sp" android:gravity="center_vertical" android:paddingLeft="10dp" /> <Button android:id="@+id/follow" android:layout_width="wrap_content" android:layout_height="match_parent" android:textSize="14sp" android:gravity="center_vertical|right" android:paddingRight="10dp" android:paddingLeft="10dp" /> </LinearLayout> </LinearLayout> </com.sothree.slidinguppanel.SlidingUpPanelLayout></RelativeLayout>
解决方法 1:
重复的 ID 问题被有关使用嵌套的片段不正确方式。
来自: http://developer.android.com/about/versions/android-4.2.html#NestedFragments
注:你不能膨胀布局到一个片段当该布局包含
<fragment>
。动态地添加到一个片段时只支持嵌套的片段。
所以您 fragment_map.xml
不能直接包含 com.google.android.gms.maps.MapFragment
。替换它的 FrameLayout
id 为 map_container
和尝试像这样:
public class MyFragmentWithMap extends Fragment { private MapFragment fragment; private GoogleMap map; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_map, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); FragmentManager fm = getChildFragmentManager(); fragment = (MapFragment) fm.findFragmentById(R.id.map_container); if (fragment == null) { fragment = MapFragment.newInstance(); fm.beginTransaction().replace(R.id.map_container, fragment).commit(); } } @Override public void onResume() { super.onResume(); setUpMapIfNeeded(); } // ...}
请注意,使用 getChildFragmentManager
。
用 FragmentManager 替换时使用 GoogleMaps 崩溃 app
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。