[英]fragment.getView() return null after backpressed
I try to change textview
under attached fragment
on activity so i stored attachedFragment
as variable as code below 我尝试在活动的附加
fragment
下更改textview
,因此我将attachedFragment
fragment
作为变量存储为以下代码
@Override
public void onAttachFragment(android.support.v4.app.Fragment fragment) {
super.onAttachFragment(fragment);
attachedFragment = fragment;
}
then when a button is clicked I call following code 然后当单击按钮时,我调用以下代码
if(attachedFragment != null && attachedFragment.getView() != null)
{
TextView tvGender = (TextView) attachedFragment.getView().findViewById(R.id.tv_gender);
if(tvGender!=null)
tvGender.setText(R.string.title_step_one_gender);
}
When I start the activity and it works fine until i changed into the next fragment and pressed back; 当我开始活动时,它工作正常,直到我切换到下一个片段并按回去; the
attachedFragment.getView()
always returns null attachedFragment.getView()
始终返回null
My question: 我的问题:
nb: All code above are under main activity nb:以上所有代码均在主要活动下
Please correct me if I misunderstood your question. 如果我误解了您的问题,请指正。 It sounds like your situation is, you attach fragment A, then you attach fragment B, then you press back, leaving you with fragment A. In this case, attachedFragment is just a variable, so it continues to point to B, but since B is now detached, it is null.
听起来您的情况是,先附加片段A,然后附加片段B,再按回去,剩下片段A。在这种情况下,attachmentFragment只是一个变量,因此它继续指向B,但是由于B现在已分离,为空。 Pressing back will not repopulate the variable attachedFragment with fragment A.
按下将不会用片段A重新填充变量AttachedFragment。
Try using findFragmentById or findFragmentByTag. 尝试使用findFragmentById或findFragmentByTag。 Check out this thread for more info.
查看此线程以获取更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.