-1

I have problems with this type of code

@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);
}

It happens in all code where is some switch/case. On every:

case R.id.XXX

eclipse returns me an error:"case expressions must be constants expressions"

Then I tried to delete R.java, clean, and regenerate it, but didn't work. How can I fix it?

Thats part of the 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>
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Shudy
  • 7,806
  • 19
  • 63
  • 98
  • When you use switch case always try to use break instead of return. – Haresh Chhelana Nov 05 '14 at 09:35
  • I know @HareshChhelana .. but this code is not mine... And I'm trying to clean all, and solve that type of error that seems when was created didn't happened..... Some parts of it are scary.... :S – Shudy Nov 05 '14 at 09:40

2 Answers2

2

This problem I had happens to this reason:

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;

The solution to this type of problems is convert "switch-case" to "if-else"

(see more at: http://tools.android.com/tips/non-constant-fields)

Original:

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;
    }

Solution

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;
        }
Shudy
  • 7,806
  • 19
  • 63
  • 98
-1

code like below to solve assign menuId in final int and then use it in 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);
}
Kirtan
  • 1,782
  • 1
  • 13
  • 35
  • Doesn't work, it's marks switch case with R.id.menu_XXX with the same error. One "autocorrect" option that eclipse gives to me is use "if-else".... but not to sure to use in this way, – Shudy Nov 05 '14 at 09:28
  • Well.. the solution is convert the switch/case to if-else... I'll add to my answer – Shudy Nov 05 '14 at 09:31