我已将按钮的可见性设置为“已消失”,但现在当我想设置按钮可见时,在尝试设置按钮的可见性时出现以下错误
@Bind(R.id.btn1)
Button btn1;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.hifragment_main, container, false);
if(1==1)
{
btn1.setVisibility(View.VISIBLE);
}
return view;
}
答案 0 :(得分:4)
您没有初始化按钮,请按以下方式初始化,
View view = inflater.inflate(R.layout.hifragment_main, container, false);
btn1 = (Button) view.findViewById(R.id.btn1);
答案 1 :(得分:0)
您需要初始化按钮对象。
btn1 = (Button) findViewById(R.id.your_btn_id);
然后您可以查看if (btn1 != null)
答案 2 :(得分:0)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.hifragment_main, container, false);
btn =view.findViewById(R.id.btn) // this line is important
btn.setVisibility(View.VISIBLE);
return view;
}
//it's work for me .
`