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:
Which I guess is on the
onCreatemethod, and previous to those lines you should have anothersetContentView(R.layout.anotherstuff);. Then, you have aloadAddEntrymethod withsetContentView(R.layout.dataentry);. So, here you have a problem: if you have already defined another contentview, why are you redefining it?