我正在尝试获取在 onCreateView () (Fragment) 中创建的视图以通过 findviewbyId 修改组件但始终返回 null
例如
这是我片段中的 onCreateView
public View onCreateView(final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myLayout, container, false);
return view;
}
并从我的活动调用片段中添加例如。
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment newFragment = new myFragment();
ft.replace(main_container.getId(), newFragment).commit();
/*and i need modify the text button with setText() of View create in my fragment
eg*/
View myViewFragment = newFragment.getView();
Button b = (Button) myViewFragment.findViewById(R.id.mybuttoninfragment);
b.setText("Hello World");
但总是返回null,我该如何解决这个问题?谢谢