why do I keep getting null exceptions at setOnClickListener
Why do I kee[ getting null errors at setOnClickListener?
R.layout.dataentry is the ContentView. It has the addRecord (a button) and it loads and displays fine. It looks as though R.id.addRecord gets an ID when I look in the debugger). I'm sure it has something to do with the ContentView not being loaded correctly resulting in a null pointer exception when I try to add the listenter, but I've tried preloading it several ways (here, earlier) and I can't figure out how to do it. I guess I'd prefer to have all my views cached so that I can add listeners early. Can someone help?Thanks.
exercise = (RadioGroup)this.findViewById(R.id.exerciseType);
addRecord = (Button)this.findViewById(R.id.addRecord);
amount = (TextView)this.findViewById(R.id.amount);
datePerformed = (DatePicker)this.findViewById(R.id.datePerfomed);
public void loadAddEntry() {
setContentView(R.layout.dataentry);
addRecord.setOnClickListener(
new View.OnClickListener(){
开发者_如何学运维 public void onClick(View view) {
addRecordClicked();
}
;});
}
You are doing it the wrong way... you have something like:
exercise = (RadioGroup)this.findViewById(R.id.exerciseType);
Which I guess is on the onCreate
method, and previous to those lines you should have another setContentView(R.layout.anotherstuff);
. Then, you have a loadAddEntry
method with setContentView(R.layout.dataentry);
. So, here you have a problem: if you have already defined another contentview, why are you redefining it?
精彩评论