菜单项的ID在Android库项目? [英] Menu item IDs in an Android library project?

查看:618
本文介绍了菜单项的ID在Android库项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android的应用程序使用的库项目,包含大部分的应用程序,code,因为有从核心源代码建立了两个版本的应用程序中。由于一个IntelliJ IDEA的更新(V11),我得到的每一个case语句下面这样的警告:

The Android app uses a library project to contain most of the app code, as there are two versions of the app built from the core source. Since an IntelliJ IDEA update (to v11) I'm getting this warning on each of the case statements below:

Resource IDs cannot be used in a switch statement in Android library modules

这里的code:

Here's the code:

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_item_one:   // Build error here
            // Do stuff
            return true;
        case R.id.menu_item_two:   // Build error here
            // Do stuff
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

确定,因此,如果无法通过其ID引用它们,我怎么引用他们呢?

OK, so if I can't reference them via their ID, how DO I reference them?

推荐答案

替换为开关的if / else,如果构建。

int id = item.getItemId();
if(id == R.id.menu_item_one) {
    // ...
}
else if(id == R.id.menu_item_two) {
    // ...
}

这是因为ADT 14 neccessary因为最后的修改是从ID的删除在R类。

This is neccessary since ADT 14 because the final modifier was removed from id's in the R class.

查看非定域的case标签

这篇关于菜单项的ID在Android库项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆