Note to all: This may be a duplicate question but I could not find a question with an answer that actually helped me out so I was forced to make this.

I am folowing the tutorial on how to make an Android application and I am stuck on this section: https://developer.android.com/training/basics/actionbar/adding-buttons.html

I am stuck on "Respond to Action Buttons" because action_search cannot be resolved or is not a field in this code:

@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);
    }
}
public void openSearch(){

}
public void openSettings(){

}

This line

case R.id.action_search:

This is hindering the progress of my learning and I would like to know how to fix it. To me it just seems like poor tutorial teaching on Android's part but it could be my fault too. Thanks!

EDIT: My XML:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          android:showAsAction="ifRoom" />
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />
</menu>
有帮助吗?

解决方案

Just had this exact problem. The tutorial fails to mention these two steps.

If you haven't yet, do the following:

  • Add an image named ic_action_search.png to the 4 different res/drawable folders
  • Create a string named action_search in strings.xml

    <string name="action_search">Search</string>
    

The main_activity_actions.xml fails to find those two things and doesn't add action_search to the R.java file, but doesn't give a useful error to point you in that direction.

Good luck!

EDIT: Also make sure you're using the app:showAsAction. As explained here.

其他提示

What IDE you are using, if it's NetBeans, remove the line contains R.id.action_search and build your project, then add this line, if u are using Android Studion, i think it needs to work, but dont import R from android, import it from your project main package, I think the probleme is in the package where u defined your activity

The above answer (dmarsi) is 100% correct.

The Android Developers tutorial mentions the following:

http://developer.android.com/training/basics/actionbar/adding-buttons.html

The icon attribute requires a resource ID for an image. The name that follows @drawable/ must be the name of a bitmap image you've saved in your project's res/drawable/ directory. For example, "@drawable/ic_action_search" refers to ic_action_search.png. Likewise, the title attribute uses a string resource that's defined by an XML file in your project's res/values/ directory, as discussed in Building a Simple User Interface.

I struggled with the same problem until I created the string and added a random .png image for ic_action_search.png

Good luck with the rest of the tutorial!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top