tags:

views:

78

answers:

1

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();
                    }

                    ;});

            }
+1  A: 

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?

Cristian
I am redefining it because it is a second form. The first is a login form, then I want to use "add an entry" form. The first one loads ok, submits ok, etc. The second one loads ok, until I add the part about the ClickListener. There doesn't seem to be an issue with the way it is being created, unless there is something I don't understand. Am I supposed to be doing this differently?
Jeff
You mean those are on different activities? Can you please paste the whole activities code?
Cristian
They are on the same activity for simplicity. I put in all my code.
Jeff
What is comes down to is that I needed to step in line and make each form its own activity.
Jeff
Well... so that's what is wrong. You cannot be switching the ContentView whenever you want. If you have an external layout and want to use it, you have to inflate it (using the `LayoutInflater`). Google is your friend in this case.
Cristian