我想在课堂上创建我的按钮,但是 findViewById未知,我需要更改什么?
public static class PlaceholderFragment extends Fragment {
private Button btn_q_c_1 = (Button)findViewById(R.id.btn_q_c_1);
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_app_launch, container, false);
Toast.makeText(getActivity(), "TEST", Toast.LENGTH_LONG).show();
return rootView;
}
}
答案 0 :(得分:1)
更改为
private Button btn_q_c_1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_app_launch, container, false);
btn_q_c_1 = (Button)rootview.findViewById(R.id.btn_q_c_1);
Toast.makeText(getActivity(), "TEST", Toast.LENGTH_LONG).show();
return rootView;
}
假设按钮属于fragment_app_launch.xml
,您需要在布局中进行扩充,并且需要在当前视图层次结构中查找该视图。
http://developer.android.com/reference/android/view/View.html#findViewById(int)
public final查看findViewById(int id)
在API级别1中添加
查找具有给定ID的子视图。如果这 view具有给定的id,返回此视图。
参数id要搜索的ID。
返回层次结构中具有给定id的视图或null
或onActivityCreated
使用
btn_q_c_1 = (Button)getView().findViewById(R.id.btn_q_c_1);