tags:

views:

74

answers:

3

Hello,

I have the following code that shows a panel. It displays a button on the panel but as soon as I assign a Click Handler to it the app crashes!

It crashes on the line .setOnClickListener

Button button =  (Button)findViewById(R.id.buttonclick);
button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                  hide();

                }
              });

LogCat shows... 09-22 14:54:09.953: ERROR/Error(7786): java.lang.NullPointerException 09-22 14:54:09.953: ERROR/Error(7786): at com.pinkfroot.leefinder.leeFinderMain$PopupPanel.(leeFinderMain.java:598)

Adding a breakpoint further down shows that R.id.buttonclick has an id but button is null.

+1  A: 

From your error it sounds as if the Button returned by findViewById is coming back as a null reference. In which case your problem will be somewhere within findViewById (or the parameter passed to it).

DrDipshit
Yes I can see that however there are many other things (not buttons) using findViewById that work fine.
Lee Armstrong
A: 

At which point do you have setContentView(R.layout.main);. This code has to be invoked before you can access your button via Button button = (Button)findViewById(R.id.buttonclick);

quaeckling
I have that in my onCreate method and then the button code is in a different class within that called "PopupPanel"
Lee Armstrong
A: 

Hi Lee, maybe you should show more code here!

quaeckling
Not sure what else I can provide, am using the commonsware example popuppanel and just want to add a button!
Lee Armstrong
Ok, I had a look on that example and see now that there are two layouts defined. The button is defined in popup.xml, I suppose. This explains the NullpointerException, because <code>findViewById()</code> is looking for that view (button) in main.xml.
quaeckling
Sorry I forgot, please try <code>popup.findViewById(R.id.buttonclick)</code> to get access to the button.
quaeckling