问题
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 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);
}
}
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:
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:
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();
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 ... ...
来源:https://stackoverflow.com/questions/46638540/fragment-to-fragment-interface-and-transaction-replace