tags:

views:

27

answers:

1

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?

+2  A: 

Does this mean one Activity can only access one screen/layout?

Yes, you can only reference widgets that are part of the current layout of your activity.

Also, in the line above, what does the "final" mean?

final is 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:

btnShowToast = (Button) findViewById(R.id.anotherButton);

On other contexts it just means: 'this cannot be changed' and it's used to define constants.

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.

In that case, this is what you have to do:

  • For the first button, set a OnClickListener that opens your second Activity.
  • Your second Acticity will use screen2.xml
  • From your second Activity, set a OnClickListener to your second button that toasts a message.
Cristian
Thanks Cristian that makes things quite clear.
Hein du Plessis
I'm having problems defining my second activity. How does one go about it? Also, how does one "activate" the second activity as it where. Thanks again.
Hein du Plessis