Reputation: 1639
I'm going through the Android tutorials, and I'm having trouble with one of the resource IDs.
In the res\menu\main.xml
file, I have an action_entry
defined in the menu tag as follows:
<item
android:id="@+id/action_search"
android:icon="@drawable:ic_action_search"
android:title="@string:action_search"
app:showAsAction="ifRoom" />
In the res\values\string.xml
file, I have the following string defined:
<string name="action_search">Search</string>
Finally, in the OnOptionsItemSelected()
function, I have the following code that references that ID:
switch (item.getItemId()) {
case R.id.action_search:
//openSearch();
return true;
...
}
According to an earlier lesson in the tutorial, the @+id means that the ID will be generated when the app is compiled, so naturally I'm expecting the case R.id.action_search:
line to be in error until then; however, when I go to run the app (which I thought would trigger a compile whenever necessary), I get an error message stating that my project contains errors that have to be fixed before running the app.
How do I force a compile of the app so that the ID is added to the generated file that contains the IDs in my project? Without an actual compile command, I'm kind of stuck in a catch-22 situation: I can't run the app until I fix the error, but to fix the error, I have to compile the app so that it generates the ID, and the only way I know to do that is to run the app. Alternatively, how do I make the IDE generate the new ID so that the app compiles when I run it?
Upvotes: 0
Views: 64
Reputation: 8138
The String names are in R.string
class, not in R.id
.
Edit: OK, you have also an ID with the same name, then it should find it... try cleaning/rebuilding the project.
Also, if there are any errors in XML files, you have to fix them to get the generated Java files.
Upvotes: 1
Reputation: 4471
It should automatically update the R.java
file, even if you are using @+id/
. However, it will not automatically update if there is an issue in one of your XML files.
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
app:showAsAction="ifRoom" />
Try this.
Upvotes: 1