Carlos
Carlos

Reputation: 59

Android Development: Undefined Method

Hi I´m new to Android and Eclipse. I have just following the tutorial from developer.android.com. Right now I´m in adding ActionBar

Right now I´m at this part

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_search:
            openSearch();
            return true;
        case R.id.action_settings:
            openSettings();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

I have received an error for openSearch() and openSettings(). It said that The method openSettings() is undefined for the type DisplayMessageActivity. What shoud I do now? Thanks

Upvotes: 3

Views: 8304

Answers (6)

Jem003
Jem003

Reputation: 41

The methods openSearch() and openSettings() should be defined. Use the following code. It'd help..

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch(id){
    case R.id.action_search :
        startActivity(new Intent(Settings.ACTION_SEARCH_SETTINGS));
        return true;
    case R.id.action_settings :
        startActivity(new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS));
        return true;
    default :
    return super.onOptionsItemSelected(item);
    }
}

Upvotes: 2

Sambuxc
Sambuxc

Reputation: 479

Im up to the same section as you, they haven't provided the methods but you have to implement them as stated above.

However I found code to open up the device settings using this code in the switch;

case R.id.action_settings:
        startActivity(new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS));
        return true;

Upvotes: 3

Samuel
Samuel

Reputation: 17171

Those two methods are just examples how selecting an option can start an action. The implementation was not provided because it was irrelevant to the example. Note that it is not a tutorial, but a single and un-compile-able example of how to add behavior to an options item.

Upvotes: 1

mah
mah

Reputation: 39807

define them.

You're basing your code on an incomplete snippet. That snippet makes no expectation of what it means to search or create settings in your app... that's your job to implement. This snippet is only concerned about showing you how to establish the action bar, not the whole application.

Upvotes: 2

M.Salomaa
M.Salomaa

Reputation: 63

Maybe you should code those methods?

private void  openSearch(){
    //your code here
}

private void openSettings(){
     //your code here
}

Upvotes: 1

ClaireG
ClaireG

Reputation: 1244

openSearch() and openSettings() are methods that the author of the tutorial created in order to perform other operations. Search well into the code, there must be somewhere the declaration of those methods, if the author made them visible.

They should look something like this:

public void openSearch() {
    //Do something here.
}

public void openSettings() {
    //Do something here.
}

Replacing the //Do something here with the code implementation present in the tutorial.

Upvotes: 3

Related Questions