遇到如下问题:
进入后首先默认选择ActionBar上的顾客管理Tab,点击店员管理Tab后正常显示(内容和顾客管理样式一样),再点击顾客管理Tab报错退出。
现在知道原因是由于顾客管理Tab的ListFragment有关。是因为虽然删除了顾客管理Tab的Fragment但是,Fragment还有一个子ListFragment没有被删除,第二次加载因为重复报错。
文件结构如下
MainActivity
CustomerFragmentClerkFragment
ListFragmentListFragment
报错如下
06-2723:09:33.443:E/AndroidRuntime(14181):FATALEXCEPTION:main 06-2723:09:33.443:E/AndroidRuntime(14181):android.view.InflateException:BinaryXMLfileline#6:Errorinflatingclassfragment 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.view.LayoutInflater.rInflate(LayoutInflater.java:746) 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.view.LayoutInflater.inflate(LayoutInflater.java:489) 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.view.LayoutInflater.inflate(LayoutInflater.java:396) 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.view.LayoutInflater.inflate(LayoutInflater.java:352) 06-2723:09:33.443:E/AndroidRuntime(14181): atstore.mgr.root.customer.CustomerMainFragment.onCreateView(CustomerMainFragment.java:25) 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.app.FragmentManagerImpl.moveToState(FragmentManager.java:829) 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035) 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.app.BackStackRecord.run(BackStackRecord.java:639) 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397) 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.app.FragmentManagerImpl$1.run(FragmentManager.java:426) 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.os.Handler.handleCallback(Handler.java:615) 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.os.Handler.dispatchMessage(Handler.java:92) 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.os.Looper.loop(Looper.java:137) 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.app.ActivityThread.main(ActivityThread.java:4823) 06-2723:09:33.443:E/AndroidRuntime(14181): atjava.lang.reflect.Method.invokeNative(NativeMethod) 06-2723:09:33.443:E/AndroidRuntime(14181): atjava.lang.reflect.Method.invoke(Method.java:511) 06-2723:09:33.443:E/AndroidRuntime(14181): atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 06-2723:09:33.443:E/AndroidRuntime(14181): atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 06-2723:09:33.443:E/AndroidRuntime(14181): atdalvik.system.NativeStart.main(NativeMethod) 06-2723:09:33.443:E/AndroidRuntime(14181):Causedby:java.lang.IllegalArgumentException:BinaryXMLfileline#6:Duplicateid0x7f0a000a,tagnull,orparentid0xffffffffwithanotherfragmentforstore.mgr.root.customer.CustomerTitlesFragment 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.app.Activity.onCreateView(Activity.java:4669) 06-2723:09:33.443:E/AndroidRuntime(14181): atandroid.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) 06-2723:09:33.443:E/AndroidRuntime(14181): ...19more 06-2723:09:34.734:I/Process(14181):Sendingsignal.PID:14181SIG:9
代码如下
MainActivity.java
packagestore.mgr.root; importstore.mgr.root.R; importstore.mgr.root.R.layout; importstore.mgr.root.R.menu; importstore.mgr.root.R.string; importstore.mgr.root.clerk.ClerkMainFragment; importstore.mgr.root.customer.CustomerMainFragment; importstore.mgr.root.customer.CustomerTitlesFragment; importandroid.app.ActionBar; importandroid.app.ActionBar.Tab; importandroid.app.ActionBar.TabListener; importandroid.app.Activity; importandroid.app.Dialog; importandroid.app.Fragment; importandroid.app.FragmentTransaction; importandroid.content.Context; importandroid.os.Bundle; importandroid.support.v4.app.FragmentActivity; importandroid.support.v4.app.FragmentTabHost; importandroid.util.Log; importandroid.view.Menu; importandroid.view.View; importandroid.view.Window; importandroid.widget.ArrayAdapter; importandroid.widget.FrameLayout; importandroid.widget.ListView; importandroid.widget.ToggleButton; /** *Thisdemonstrateshowyoucanimplementswitchingbetweenthetabsofa *TabHostthroughfragments,usingFragmentTabHost. */ publicclassMainActivityextendsActivity{ @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //setupActionBarfortabs finalActionBaractionBar=getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); //removetheactivitytitletomakespacefortabs actionBar.setDisplayShowTitleEnabled(false); //instantiatefragmentforthetab actionBar.addTab(actionBar.newTab().setText(R.string.customer_mgr) .setTabListener(newStoreTabListener(newCustomerMainFragment()))); actionBar.addTab(actionBar.newTab().setText(R.string.clerk_mgr) .setTabListener(newStoreTabListener(newClerkMainFragment()))); } @Override publicbooleanonCreateOptionsMenu(Menumenu){ super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.main,menu); returntrue; } privateclassStoreTabListenerimplementsActionBar.TabListener{ privateFragmentfragment; publicStoreTabListener(Fragmentfragment){ super(); this.fragment=fragment; //TODOAuto-generatedconstructorstub } @Override publicvoidonTabReselected(Tabtab,FragmentTransactionft){ //TODOAuto-generatedmethodstub } @Override publicvoidonTabSelected(Tabtab,FragmentTransactionft){ //TODOAuto-generatedmethodstub ft.add(R.id.customer_clerk,fragment); //ft.commit(); } @Override publicvoidonTabUnselected(Tabtab,FragmentTransactionft){ //TODOAuto-generatedmethodstub ft.remove(fragment); //ft.commit(); } } }
MainActivity.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:baselineAligned="false" android:orientation="horizontal" android:layout_width="match_parent"android:layout_height="match_parent"> <FrameLayoutandroid:id="@+id/customer_clerk"android:layout_weight="1" android:layout_width="0px"android:layout_height="match_parent" android:background="?android:attr/detailsElementBackground"/> </LinearLayout>
CustomerMainFragment.java
packagestore.mgr.root.customer; importstore.mgr.root.R; importandroid.app.Fragment; importandroid.app.FragmentTransaction; importandroid.os.Bundle; importandroid.util.Log; importandroid.view.LayoutInflater; importandroid.view.View; importandroid.view.ViewGroup; importandroid.widget.TextView; importandroid.widget.LinearLayout.LayoutParams; /** *Thisdemonstrateshowyoucanimplementswitchingbetweenthetabsofa *TabHostthroughfragments,usingFragmentTabHost. */ publicclassCustomerMainFragmentextendsFragment{ @Override publicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){ Viewlayout=inflater.inflate(R.layout.activity_main_customer,null); returnlayout; } }
customer_main_customer.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:baselineAligned="false" android:orientation="horizontal" android:layout_width="match_parent"android:layout_height="match_parent"> <fragmentandroid:name="store.mgr.root.customer.CustomerTitlesFragment" android:id="@+id/customer_titles"android:layout_weight="1" android:layout_width="0px"android:layout_height="match_parent"/> <FrameLayoutandroid:id="@+id/customer_details"android:layout_weight="1" android:layout_width="800dp"android:layout_height="match_parent" android:background="?android:attr/detailsElementBackground"/> </LinearLayout>
CustomerTitleFragment.java
packagestore.mgr.root.customer; importandroid.app.ListFragment; importandroid.os.Bundle; importandroid.widget.ArrayAdapter; publicclassCustomerTitlesFragmentextendsListFragment{ @Override publicvoidonActivityCreated(BundlesavedInstanceState){ //TODOAuto-generatedmethodstub super.onActivityCreated(savedInstanceState); Stringtitles[]=newString[]{"dogs","cats","horses"}; setListAdapter(newArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_activated_1,titles)); } }
clerk和Customer一样,只是内容不同
===================================================
我要用下面的代码来加载的片段的项已被选定在 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: 23 03-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 2013 03-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_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
。
路漫漫其修远兮 吾将上下而求索
原文链接:https://www.f2er.com/xml/297754.html