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