为啥空指针?

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


qq_Linjj蓝极光辉_04362138
浏览 1037回答 1
1回答

ysutm

检查下1、初始化button之前是否调了setContentView(view);2、设置的view是否包含了你那2个button的id
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android