FindFragmentById 不返回 null
我有以下代码:
//Compruebo si tengo los dosfragos
FichaAlumnoFragment frag=(FichaAlumnoFragment)getSupportFragmentManager().findFragmentById(R.id.details_students_frag);
if (frag!=null) {
Log.i(this.getClass().getSimpleName(),"Tengo dos fragmentos");
mDualFragments=true;
}
如果我处于纵向模式,它将返回 null。如果我更改为横向(模拟器中的 Ctrl+11),frag 有一个值,并且布局会正确呈现,但如果我再次更改为纵向,它应该具有空值,但会返回一些值,并且应用程序崩溃。
两个方向的布局都不同
我认为我误解了片段生命周期。有什么想法吗?
I have the following code:
//Compruebo si tengo los dos fragmentos
FichaAlumnoFragment frag=(FichaAlumnoFragment)getSupportFragmentManager().findFragmentById(R.id.details_students_frag);
if (frag!=null) {
Log.i(this.getClass().getSimpleName(),"Tengo dos fragmentos");
mDualFragments=true;
}
If I'm in portrait mode it returns null. If I change to landscape (Ctrl+11 in the emulator) frag has a value and the layout is rendered correctly but If I change again to portrait it's supposed to have a null value but returns some value and the app crashes.
Layout are different for both orientations
Think I misunderstood fragment lifecylce. Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚遇到了同样的问题。您仍然需要检查是否为 null,但还要使用方法 isInLayout() 进行检查,例如:
I just had the same issue. You still have to check for null, but then also check with the method isInLayout(), for instance:
好吧,我找到了解决方案..至少有一个对我有用...
一旦我在布局(横向)中加载了一个片段,尽管我更改了方向(只有一个片段的布局),findFragmentById 始终返回 true,尽管我更改为再次肖像。如果我还检查 isInLayout 它工作正常。
找到它:
http://android10 .org/index.php/articlesuserinterface/309-learn-how-to-implement-fragments-in-android
Well i found the solution..al least one that works for me...
Once I have loaded a fragment in a layout (landscape) although I change orientation (a layout with only one fragment) findFragmentById is always returning true althoug i change to portrait once more. If I also check isInLayout it works right.
Found it at:
http://android10.org/index.php/articlesuserinterface/309-learn-how-to-implement-fragments-in-android