1
我在位于片段上的textview上设置了文本,它返回一个NullPointerException。它在静态添加片段时起作用,但当我使用动态添加片段时会崩溃。在活动的片段上设置文本时出现NullPointerException
这是当我想要设置片段上的文本时调用的函数。
@Override
public void countrySelected(String wordIn) {
word = wordIn;
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
display.setCountryText(wordIn);
}else{
display = new DisplayFragment();
//display.setCountryText(wordIn);
fragmentTransaction.addToBackStack(list.getTag());
fragmentTransaction.replace(R.id.fragment_place,display,"disp");
fragmentTransaction.commit();
display.setCountryText(wordIn);
}
}
这是片段。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right,container, false);
txt = (TextView) view.findViewById(R.id.txtView);
Log.d("LOOOOADED", "Loaded");
return view;
}
public void setCountryText(String word){
txt.setText(word);
}
当我在静态添加的片段上设置文本时,它工作得很好。但是当我在动态添加的片段上设置文本时,它不会记录“LOOOOADED”,这意味着它永远不会到达方法onCreateView()
。
啊......所以我应该什么时候打电话呢? – user3138759
@ user3138759检查工具生命周期http://developer.android.com/guide/components/fragments.html – Raghunandan
@ user3138759在'onResume'调用方法 – Raghunandan