setOnClickListener的代码:
btnComfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cartProvider.addData(goodsBean);
window.dismiss();
Toast.makeText(GoodsInfoActivity.this, "加入购物车", Toast.LENGTH_SHORT).show();
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cartProvider.addData(goodsBean);
window.dismiss();
Toast.makeText(GoodsInfoActivity.this, "取消加入购物车", Toast.LENGTH_SHORT).show();
}
});
………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
定义变量和通过findViewById找到控件:
private Button btnCancel;
private Button btnComfirm;
btnCancel = (Button)findViewById( R.id.btn_cancel );
btnComfirm = (Button)findViewById( R.id.btn_comfirm );
tvPopPrice =(TextView)findViewById(R.id.tv_pop_price);
btnCancel.setOnClickListener( this );
btnComfirm.setOnClickListener( this );
………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
报错;
Process: com.weather.com.welcomeactivity, PID: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.weather.com.welcomeactivity/com.weather.com.welcomeactivity.app.GoodsInfoActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android .widget. Button. setOnClickListener (android.view.View$OnClickListener)' on a null object reference
ysutm
相关分类