为了账号安全,请及时绑定邮箱和手机立即绑定

为啥空指针?

为啥空指针?

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
查看完整描述

1 回答

?
ysutm

TA贡献2条经验 获得超1个赞

检查下
1、初始化button之前是否调了setContentView(view);

2、设置的view是否包含了你那2个button的id

查看完整回答
反对 回复 2017-11-25
  • qq_Linjj蓝极光辉_04362138
    qq_Linjj蓝极光辉_04362138
    这个会不会少了啥 class ViewHolder extends RecyclerView.ViewHolder { public Context mContext; public ViewHolder(View view) { super(view); } **************************************************************************** 在唯一的setcontentView protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_goods_info); setContentView(R.layout.popwindow_add_product); findViews();
  • qq_Linjj蓝极光辉_04362138
    qq_Linjj蓝极光辉_04362138
    我发现我setContentView两个布局它只生效一个,另一个为空
  • 1 回答
  • 0 关注
  • 1031 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信