在android中使用R.id.XXXX切换/案例不起作用

时间:2014-11-05 09:16:02

标签: android switch-statement

我遇到这类代码的问题

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_sign_out_all:
        doHardShutdown();
        return true;
    case R.id.menu_add_account:
     //   showExistingAccountListDialog();
        return true;
    case R.id.menu_settings:
        Intent sintent = new Intent(this, SettingActivity.class);
        startActivityForResult(sintent,0);
        return true;
    case R.id.menu_import_keys:
        importKeyStore();
        return true;
   // case R.id.menu_exit:
  //      signOutAndKillProcess();

      //  return true;
    }
    return super.onOptionsItemSelected(item);
}

它发生在所有代码中的某些开关/案例。 每一个:

case R.id.XXX

eclipse给我一个错误:" case表达式必须是常量表达式"

然后我尝试删除R.java,清理并重新生成它,但是没有用。 我该如何解决?

这是xml的一部分:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto">
<item
        android:id="@+id/menu_add_account"
        android:title="@string/menu_add_account"
         android:icon="@android:drawable/ic_menu_add"
                 app:showAsAction="always|withText"
        >
</item>

2 个答案:

答案 0 :(得分:0)

代码如下解决 在final int中分配menuId然后在switch case中使用它。

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    final int menuId = item.getItemId();
    switch (menuId)
    {
    case R.id.menu_sign_out_all:
        doHardShutdown();
        return true;
    case R.id.menu_add_account:
        return true;
    case R.id.menu_settings:
        Intent sintent = new Intent(this, SettingActivity.class);
        startActivityForResult(sintent,0);
        return true;
    case R.id.menu_import_keys:
        importKeyStore();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

答案 1 :(得分:0)

我遇到这个问题的原因是:

In a regular Android project, constants in the resource R class are declared like this:
public static final int main=0x7f030004;

However, as of ADT 14, in a library project, they will be declared like this:
public static int main=0x7f030004;

解决这类问题的方法是转换&#34; switch-case&#34;到&#34; if-else&#34;

(详见:http://tools.android.com/tips/non-constant-fields

<强>原始

switch (item.getItemId()) {
    case R.id.menu_sign_out_all:
        doHardShutdown();
        return true;
    case R.id.menu_add_account:
     //   showExistingAccountListDialog();
        return true;
    case R.id.menu_settings:
        Intent sintent = new Intent(this, SettingActivity.class);
        startActivityForResult(sintent,0);
        return true;
    case R.id.menu_import_keys:
        importKeyStore();
        return true;
    }

<强>解决方案

int itemId = item.getItemId();
        if (itemId == R.id.menu_invite_user) {
            Intent i = new Intent(ContactListActivity.this, AddContactActivity.class);
            i.putExtra(ImServiceConstants.EXTRA_INTENT_PROVIDER_ID, mProviderId);
            i.putExtra(ImServiceConstants.EXTRA_INTENT_ACCOUNT_ID, mAccountId);
            i.putExtra(ImServiceConstants.EXTRA_INTENT_LIST_NAME,
                    mContactListView.getSelectedContactList());
            startActivity(i);
            return true;
        } else if (itemId == android.R.id.home || itemId == R.id.menu_view_accounts) {
            startActivity(new Intent(getBaseContext(), ChooseAccountActivity.class));
            // finish();
            return true;
        } else if (itemId == R.id.menu_settings) {
            Intent sintent = new Intent(this, SettingActivity.class);
            startActivity(sintent);
            return true;
        }