片段到片段接口和事务替换 [英] fragment to fragment interface and transaction replace

查看:172
本文介绍了片段到片段接口和事务替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个默认的android页面,其中包含3个标签和片段管理器,我已经添加了2个片段,到目前为止,一切都运行良好,直到我尝试将接口发送到主要活动并从中发送数据通过捆绑和交易到第三个片段:

I created a default android page the one with 3 tabs and fragment manager and I have added 2 more fragments to it and so far everything has been working fine till I try to send a interface to the main activity and send the data from there to the third fragment by bundle and transaction:

LIke这个:

 public void setF4Riddle(int x){
    Frag4 F4 = 
 (Frag4)getSupportFragmentManager().findFragmentById(R.id.frag4);


    if (F4 != null ) {
        F4.getF4Riddle(x);

    } else {
        Frag4 fragment = new Frag4();
        Bundle args = new Bundle();
        args.putInt("Value", x);
        TextView Dialog =(TextView)findViewById(R.id.Dialog);
        Dialog.setText("");
        fragment.setArguments(args);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.frag4_container, fragment)
                .addToBackStack(null).commit();
        fragment.getF4Riddle(x);
    }


}

它的工作原理片段4(从片段5到4)很好但是当我尝试与片段3相同的事情时它会崩溃(崩溃发生在事务替换中,它不会取代frag3布局)事实上我已经尝试过片段2和1在所有情况下游戏崩溃(1,2,3),但是当我添加更多片段(6和7)时,它再次起作用,所以看起来只有前三个不工作才有人知道原因吗?谢谢

and it works fine with Fragment 4 ( its from fragment 5 to 4) but when I try the same thing with fragment 3 it crashes (crash happens in transaction replacement , it doesnt replace the frag3 layout) in fact ive tried with fragment 2 and 1 in all cases the game crashes ( 1 ,2 ,3) but when i add more fragments (6 and 7) it works again so it seems like only the first 3 dont work does anyone know the reason? Thank You

StackTrace:

StackTrace:

10-09 00:01:46.985 20690-20690/com.redot.puzzle3 E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: com.redot.puzzle3, PID: 20690
                                                               java.lang.IllegalArgumentException: No view found for id 0x7f0d0098 (com.redot.puzzle3:id/frag3) for fragment Frag3{128ae004 #5 id=0x7f0d0098}
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1326)
                                                                   at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574)
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641)
                                                                   at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794)
                                                                   at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415)
                                                                   at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200)
                                                                   at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153)
                                                                   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2063)
                                                                   at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:725)
                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                   at android.os.Looper.loop(Looper.java:145)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:6873)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)


推荐答案

嗯,我认为问题可能是你用FragManager添加Frag3片段。当您尝试添加片段的片段容器在Activity的视图层次结构中不存在时,可能会出现此错误。例如:

Well, I think the problem could be when you add the Frag3 fragment with the fragmentManager. This error might comes up when the fragment container in which you are trying to add your fragment, is not present in the Activity's view hierarchy. For instance:

getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.fragmentContainer, frag3)
    .commit();

确保'R.id.fragmentContainer'是你指向的正确的FrameLayout,它存在于当前活动的布局中。由于您可能在不同的布局中调用片段容器,这可能导致:

make sure the 'R.id.fragmentContainer' is the right FrameLayout you are pointing to, and it is present in your current Activity's layout. Since you might be calling a fragment container in a different layout, which might result in a:

IllegalArgumentException: No view found for id 0x7f0b0061 ... for fragment ... ...

这篇关于片段到片段接口和事务替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆