A (175)

(Tuis.) #1
CHAPTER 7: Making Apps Interactive: Intents, Event Handling, and Menus 253

The following line of code produces error highlights, under the .setOnClickListener( ) method and
View class:


colonyButton.setOnClickListener(new View.OnClickListener( ) { Your .onClick( ) event handler code
will go in here });


My rule of thumb for dealing with multiple error highlights in Eclipse is to ascertain if any of them
are under class names which may not yet be imported, and to mouse-over these, and generate the
import statements first, as is shown in Figure 7-36. The logic behind why I do this is because the
other error message in the same line of Java code might be rectified (fixed and removed) by the
import of that class as well. This is indeed the case in this particular error scenario.


Now that we have erased the error highlighting under the .setOnClickListener( ) method, we still have
a more complex error highlighting under the View.OnClickListener( ) constructor method call. Mouse-
over this error, and you will see that all Eclipse wants to do is to create your .onClick( ) method
structure for you, so this is a “good” error message, and you can go ahead and select the “Add
unimplemented methods” option, which will save you some keystrokes! This will declare a public
void onClick( ) method structure, inside of the .setOnClickListener structure you are creating, and
will code an empty method for you (which you will fill later) using the following code:


@Override
public void onClick(View v) { Your Java program logic for processing the received onClick events
will go in here }


Figure 7-36. Call the .setOnClickListener( ) method off colonyButton object and create new View.OnClickListener

Free download pdf