0

当我尝试为我创建的按钮实现 onclicklistener 时,我不断收到空指针异常。

    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;
        }
    }
4

2 回答 2

0

写这篇文章的时候你期待什么?

Button setHighBtn[] = null;
Button setLowBtn[]=null;
于 2012-08-14T22:02:42.673 回答
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);
于 2012-08-14T22:13:36.327 回答