当我尝试为我创建的按钮实现onclicklistener时,我一直得到一个nullpointer异常。
Button setHighBtn[] = null;
Button setLowBtn[]=null;
TextView tv[] = new TextView[oNumber];
for (int i=0;i<oNumber;i++){
tv[i] = new TextView(this);
tv[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv[i].setText(oName[i]);
tv[i].setTextSize(20);
//tv[i].setLayoutParam)
layout.addView(tv[i]);
if (capability[i]==0){
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.full_capabilities, layout);
setHighBtn[i]=(Button)findViewById(R.id.setHigh);
setHighBtn[i]=(Button)findViewById(R.id.setLow);
setHighBtn[i].setOnClickListener(this);
setLowBtn[i].setOnClickListener(this);
}else{
setHighBtn[i]=null;
setLowBtn[i]=null;
}
}
答案 0 :(得分:0)
写这篇文章时你有什么期望?
Button setHighBtn[] = null;
Button setLowBtn[]=null;
答案 1 :(得分:0)
我认为这是一个复制粘贴错误。你写道:
setHighBtn[i]=(Button)findViewById(R.id.setHigh);
setHighBtn[i]=(Button)findViewById(R.id.setLow); //This is supposed to be setLowBtn[i]
然后将听众设置为尚未初始化的setLowBtn
。将其编辑为:
setHighBtn[i]=(Button)findViewById(R.id.setHigh);
setLowBtn[i]=(Button)findViewById(R.id.setLow);