在 Fragment 中,不能直接使用 'findViewById' 方法来获取视图元素,正确的做法是在 Fragment 的 onCreateView 方法中,使用根视图对象调用 'findViewById' 方法
解决方法
public class FragmentOne extends Fragment {
private View rootView;
private TextView textView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_one, container, false);
textView = rootView.findViewById(R.id.text_view);
// 在这里可以对 textView 进行操作
//也可以封装一下,另外去写方法进行调用
return rootView;
}
}