在 Fragment 中以编程方式创建复选框
Create checkbox programmatically in Fragment
我正在尝试在我的 Fragment 中创建一个复选框。我没有让它正常工作。
这是我的片段代码:
public class DriverFragment extends Fragment {
public DriverFragment(){
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CheckBox check = new CheckBox(getActivity());
check.setText("ff");
check.setPadding(10,10,10,10);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(5,5,5,5);
params.gravity = Gravity.NO_GRAVITY;
check.setLayoutParams(params);
check.setGravity(Gravity.CENTER);
LinearLayout lin = (LinearLayout)getView().findViewById(R.id.driverLayout);
lin.addView(check);
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_driver,container,false);
}
}
我收到这个错误:
Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
我得到这个是因为带有 LinearLayout 的视图尚未加载吗?
参见 片段生命周期 你无法在 OnCreate()
中获取视图小部件,因为它在 OnCreateView();
中膨胀
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_driver, container, false);
CheckBox check = new CheckBox(getActivity());
check.setText("ff");
check.setPadding(10, 10, 10, 10);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
params.gravity = Gravity.NO_GRAVITY;
check.setLayoutParams(params);
check.setGravity(Gravity.CENTER);
LinearLayout lin = (LinearLayout) view.findViewById(R.id.driverLayout);
lin.addView(check);
return view;
}
我正在尝试在我的 Fragment 中创建一个复选框。我没有让它正常工作。 这是我的片段代码:
public class DriverFragment extends Fragment {
public DriverFragment(){
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CheckBox check = new CheckBox(getActivity());
check.setText("ff");
check.setPadding(10,10,10,10);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(5,5,5,5);
params.gravity = Gravity.NO_GRAVITY;
check.setLayoutParams(params);
check.setGravity(Gravity.CENTER);
LinearLayout lin = (LinearLayout)getView().findViewById(R.id.driverLayout);
lin.addView(check);
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_driver,container,false);
}
}
我收到这个错误:
Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
我得到这个是因为带有 LinearLayout 的视图尚未加载吗?
参见 片段生命周期 你无法在 OnCreate()
中获取视图小部件,因为它在 OnCreateView();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_driver, container, false);
CheckBox check = new CheckBox(getActivity());
check.setText("ff");
check.setPadding(10, 10, 10, 10);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
params.gravity = Gravity.NO_GRAVITY;
check.setLayoutParams(params);
check.setGravity(Gravity.CENTER);
LinearLayout lin = (LinearLayout) view.findViewById(R.id.driverLayout);
lin.addView(check);
return view;
}