我是Android开发的新手,当然还有Fragments。
我有下面的代码,我想在片段中使用findViewById,但因为它在onCreateView上有错误
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_main,null);
x2=(Button)getView().findViewById (R.id.btnX2);
x2.setText(Html.fromHtml(getResources().getString(R.string.X2)));
showResult = (EditText)getView().findViewById(R.id.display);
}
答案 0 :(得分:0)
你可以试试这个。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.activity_main,container,false);
x2=(Button)v.findViewById (R.id.btnX2);
x2.setText(Html.fromHtml(getResources().getString(R.string.X2)));
showResult = (EditText)v.findViewById(R.id.display);
return v;
}
答案 1 :(得分:0)
试试这个会帮助你
View view=inflater.inflate(R.layout.activity_main,null);
x2=(Button)view.findViewById (R.id.btnX2);
x2.setText(Html.fromHtml(getResources().getString(R.string.X2)));
showResult = (EditText)view.findViewById(R.id.display);
return view;