当前 MainFragment中创添加多个Fragment代码如下:
MainFragment的Layout文件main_dialog_fragment
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" androID:ID="@+ID/ll_root_vIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="@color/white" androID:clickable="true" androID:fitsSystemwindows="true" androID:focusableIntouchMode="true" androID:orIEntation="vertical"> //...省略无关代码 <FrameLayout androID:ID="@+ID/fragment_container" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="@androID:color/white"> </FrameLayout> </linearLayout></linearLayout>
MainFragment.Java文件如下:
@Nullable @OverrIDe public VIEw onCreateVIEw(@NonNull LayoutInflater inflater, @Nullable VIEwGroup container, @Nullable Bundle savedInstanceState) { initParams(); VIEw rootVIEw = inflater.inflate(R.layout.main_dialog_fragment, container, false); initFragments(savedInstanceState); return rootVIEw; } //初始化两个Fragment 并且显示第一个 private voID initFragments(@Nullable Bundle savedInstanceState) { FragmentActivity fragmentActivity = getActivity(); if (fragmentActivity == null) { return; } //savedInstanceState是对Bundle对象的引用,可以使用此捆绑包中存储的数据将自身恢复到以前的状态 if (savedInstanceState == null) { try { //初始化Fragment m_fragment1 = new PaymentSubFragment1(); m_fragment2 = new PaymentSubFragment2(); fragmentActivity.getSupportFragmentManager().beginTransaction() .add(R.ID.fragment_container, m_fragment1, kTagFragment1) .add(R.ID.fragment_container, m_fragment2, kTagFragment2) .detach(m_fragment2) .commit(); }catch (Exception e){ Utils.printBBException(e,279); } //设定当前的Fragment,知道下一次hIDe那个Fragment m_CurFragment = m_fragment1; } else { //由于FragmentManager已经存在了这些Fragment直接获取就可以了 m_fragment1 = (PaymentSubFragment1) fragmentActivity.getSupportFragmentManager() .findFragmentByTag(kTagFragment1); m_fragment2 = (PaymentSubFragment2) fragmentActivity.getSupportFragmentManager() .findFragmentByTag(kTagFragment2); } }
这时运行会报出如下错误:
E/AndroIDRuntime: FATAL EXCEPTION: main Process: com.xxx.androID, PID: 29708 java.lang.IllegalArgumentException: No vIEw found for ID 0x7f08030b (com.xxx.androID:ID/fragment_container) for fragment PaymentSubFragment1{3820d34 (412762d8-5144-47fd-9ff7-abf3ce161066) ID=0x7f08030b kTagFragment1} at androIDx.fragment.app.FragmentManagerImpl.movetoState(FragmentManagerImpl.java:875) at androIDx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238) at androIDx.fragment.app.FragmentManagerImpl.movetoState(FragmentManagerImpl.java:1303) at androIDx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439) at androIDx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079) at androIDx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869) at androIDx.fragment.app.FragmentManagerImpl.removeRedundantoperationsAndExecute(FragmentManagerImpl.java:1824) at androIDx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727) at androIDx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150) at androID.os.Handler.handleCallback(Handler.java:883) at androID.os.Handler.dispatchMessage(Handler.java:100) at androID.os.Looper.loop(Looper.java:264) at androID.app.ActivityThread.main(ActivityThread.java:7581) at java.lang.reflect.Method.invoke(Native Method) at com.androID.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:980)
提示原因找不到VIEw 名称为 fragment_container, 实际上Layout文件中是存在的,原因是在MainFragment中使用了getSupportFragmentManager方法,因为getSupportFragmentManager方法是属于FragmentActivity类的,因为查找的是Activity的Layout文件所以找不到这个VIEw, 将其改为getChildFragmentManager就是当前MainFragment的Layout了,修改后代码如下:
//初始化两个Fragment 并且显示第一个 private voID initFragments(@Nullable Bundle savedInstanceState) { if (savedInstanceState == null) { try { //初始化Fragment m_fragment1 = new PaymentSubFragment1(); m_fragment2 = new PaymentSubFragment2(); getChildFragmentManager().beginTransaction() .add(R.ID.fragment_container, m_fragment1, kTagFragment1) .add(R.ID.fragment_container, m_fragment2, kTagFragment2) .detach(m_fragment2) .commit(); }catch (Exception e){ Utils.printBBException(e,279); } //设定当前的Fragment,知道下一次hIDe那个Fragment m_CurFragment = m_fragment1; } else { //由于FragmentManager已经存在了这些Fragment直接获取就可以了 m_fragment1 = (PaymentSubFragment1) getChildFragmentManager() .findFragmentByTag(kTagFragment1); m_fragment2 = (PaymentSubFragment2) getChildFragmentManager() .findFragmentByTag(kTagFragment2); } }
总结 以上是内存溢出为你收集整理的Android MainFragment中使用多个Fragment全部内容,希望文章能够帮你解决Android MainFragment中使用多个Fragment所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)