添加button和onclicklistener后Attempt to invoke virtual method on a
(2016-01-16 18:21:34)
标签:
androidbuttoncontent |
分类: 技巧和技术 |
setContentView这个方法一定要在findViewById方法之前,不然就会出错:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//this method should be in front of the findViewById
startSensor=(Button)findViewById(R.id.startSensorButton);
startSensor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}