我有两个按钮,addBtn和declineBtn。单击其中任何一个时,我想禁用这两个按钮。我一直在
尝试调用虚方法' void android.widget.Button.setEnabled(布尔值)'在空对象引用上
问题是,如果我点击拒绝按钮,那么onClicklistener v.findViewById找不到添加按钮。任何帮助将不胜感激
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if(rowView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
rowView = inflater.inflate(R.layout.friend_request_item, parent, false);
}
final FriendRequest request = getItem(position);
TextView userId = (TextView) rowView.findViewById(R.id.textView1);
userId.setText(request.getRequesterId());
Button acceptBtn = (Button) rowView.findViewById(R.id.button1);
acceptBtn.setTag(request.getRequesterId());
Button declineBtn = (Button) rowView.findViewById(R.id.button2);
declineBtn.setTag(request.getRequesterId());
acceptBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Button AddBtn = (Button) v.findViewById(R.id.addbutton1);
AddBtn.setEnabled(false);
Button declineBtn = (Button) v.findViewById(R.id.devlinebutton2);
declineBtn.setEnabled(false);
}
});
declineBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Button AddBtn = (Button) v.findViewById(R.id.addbutton1);
AddBtn.setEnabled(false);
v.setEnabled(false);
Button declineBtn = (Button) v.findViewById(R.id.declinebutton2);
declineBtn.setEnabled(false);
}
});
return rowView;
}
答案 0 :(得分:0)
您不应使用v
来致电findViewById()
。例如,只需编写(Button) findViewById(R.id.button1)
。
答案 1 :(得分:0)
我相信按钮位于rowView
内,所以我认为您应该使用rowView.findViewById()
代替v.findViewById()