片段到片段接口和事务替换

时间:2017-10-09 03:45:01

标签: android interface

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

LIke This:

 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);
    }


}

并且它与Fragment 4(它从片段5到4)工作正常但是当我尝试使用片段3进行相同的操作时崩溃(崩溃发生在事务替换中,它不会替换frag3布局)实际上我已尝试使用片段2和1在所有情况下游戏崩溃(1,2,3),但当我添加更多的片段(6和7),它再次工作,所以似乎只有前3个不工作有没有人知道原因?谢谢

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)

1 个答案:

答案 0 :(得分:0)

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

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

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

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