我通常创建initialize()
用于设置点击监听器的函数和函数,然后我从 Activity 的onCreate()
. 当我调用someView.setOnClickListener(...)
一个函数时,我应该检查是否someView == null
?我知道,!= null
但我需要关于编码风格的建议。哪个是最佳实践?
这是一个例子:
...
public class SomeActivity extends Activity
{
private ImageButton someButton;
private Intent someIntent;
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.add_alarm);
initialize();
setSomeButtonHandler();
}
public void initialize()
{
someButton = (ImageButton) findViewById(R.id.someButton);
}
public void setSomeButtonHandler()
{
if(someButton != null) //Should I check this?
{
someIntent = new Intent(SomeActivity.this, SomeButtonActivity.class);
someButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
startActivity(someIntent);
}
});
}
}
}