I have 开发者_JS百科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.
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).
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);
精彩评论