I’m expanding on the hello world application. I’ve created a button on the main.xml to bring up a new form, screen2.xml, with a second button. I’d like this this button to toast a message. However, as soon as I try add the code to define the OnClickListener, I get a force close message. It happens on this line of code:
final Button btnShowToast = (Button) findViewById(R.id.btnShowToast)
Does this mean one Activity can only access one screen/layout?
Also, in the line above, what does the “final” mean?
Yes, you can only reference widgets that are part of the current layout of your activity.
finalis a Java reserved keyword which has slightly different meanings depending on the context. In this case, it means that you cannot assign another reference to the button. For instance, you cannot do this after that line:On other contexts it just means: ‘this cannot be changed‘ and it’s used to define constants.
In that case, this is what you have to do:
OnClickListenerthat opens your secondActivity.Acticitywill usescreen2.xmlActivity, set aOnClickListenerto your second button that toasts a message.