Switch - case expressions must be constant expressions - android

I'm using ActionBarSherlock and then onOptionsItemSelected to start a new activity when a specific menu item has been clicked. The code worked properly before adding ABS, and now I get case expressions must be constant expressions error on case.
#Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item)
{
switch (item.getItemId()) {
case R.id.about: //error
startActivity(new Intent(this, AboutActivity.class));
break;
case R.id.feedback: //error
//launch activity
break;
default:
break;
return super.onOptionsItemSelected(item);
}
The same code worked fine before adding ActionBarSherlock.

I've replaced the switch/case statement with if/else. You can just click on switch and then press CTRL+1 if you're in Eclipse.

Posted as an answer as Sam adviced:
If you are in a library you have to change all the switch/case statements to if/else blocks from ADT version 14.
See:
tools.android.com/tips/non-constant-fields

To switch from the switch/case statement to if/for, simply use alt + enter in android studio.

Switch case missing closing "}".

Related

Build error: Cannot find symbol. Points to navigation actions

I keep getting an error that says that it cannot find the navigation actions. Here is the place that it gives me errors:
public void onItemClick(String item) {
switch (card) {
case ACCESSORIES:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToAccessories(item));
case ARMOR_PIECES:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToArmorPieceType(item));
case ARMOR_SETS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToArmorSets(item));
case DESTINIES:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToDestinyList(item));
case ENEMIES:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToEnemyList(item));
case GEMS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToGemList(item));
case LORESTONES:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToLoreSets(item));
case POTIONS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToPotionTypes(item));
case QUESTS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToQuestList(item));
case SKILLBOOKS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToSkillbook(item));
case TRAINERS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToTrainerList(item));
case TWISTS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToTwistList(item));
case WEAPONS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToWeaponList(item));
It gives me errors on all 13 navigation actions. Here is my gradle: https://pastebin.com/jaK6CK7z
And finally here is the navigation file: https://pastebin.com/8DyR7vpj
As far as I can tell, I have all the elements needed. Any help is greatly appreciated.
I figured out the problem. I was trying to navigate to destinations that were in a nested NavGraph. I found that I did not need to have nested graphs, so taking out the graphs and leaving the fragments was all I needed to do.

onClick() does not work after appying dexguard. Is there any way to solve this?

i am using dexguard to secure my app. Recently i updated the dexgaurd version
from 8.0.1 to 8.2.15. Earlier everything is working fine before the update. But with the version 8.2.15 when i apply dexguard, onCick method does not works in one of fragment SettingsFragment, for all of the other Fragments it works fine. However the code and method of implementing onClick() is same for all Fragments. But for SettingsFragment it's not working. Please help.
Here is my onClick method in SettingsFragment
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.relSignOut:
mCallback.doSignOut();
break;
// case R.id.relEditProfile:
// loadManageProfile();
// break;
case R.id.btn_edit_profile:
loadManageProfile();
break;
case R.id.relDynamicFxRate:
parent.startSetExchangeAlertActivity();
break;
}
}
};
Thanks in Advance
You should exclude obfuscation of onClick methods like this:
-keepclassmembers class * {
public void onClick (android.view.View);
}
(Converting my comment to answer)
Please kick off the switch statement and replace it with if-else. May seem bit unscientific and illogical, but I worked for me many times.
I don't know if it's a possible bug in the compiler or in Android or not, but sometimes rejecting switch statement only doesn't help. Then I've to replace view.getId() with view.getPosition() and check by order or something like this to make it work anyway.
From T.Neidhart's comment: The reason it fails is probably due to resource optimization which remaps resource IDs. Normally these remapped resource IDs get replaced everywhere in the code, but it might go wrong in case of switch statements. You can disable resource optimization like that: -optimizations resource/compaction –

Warning on setOnTouchListener() of all Views

Recently after updating Android Studio to 3.0 and everything was working fine, but from today it shows warning on every setOnTouchListener() of any view also the logic inside it dose not seem to work properly anymore, i don't exactly remember if i have updated any library.
Warning message:
Custom View 'NestedScrollView' has setOnTouchListener called on it but does not override performClick
After searching on Stackoverflow i implemented following solution but it did not remove the warning.
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
//some code....
break;
case MotionEvent.ACTION_UP:
view.performClick();
break;
default:
break;
}
Any idea why this is happening?
Too late with an answer, but for others who is running into the same issue. Actually, the solution is given in the warning text: you need to override performClick() method in your class. Just put this code in it:
#Override
public boolean performClick() {
return super.performClick();
}

android studio: type annotations are not supported

I simply referred to an already defined string, like this
Entry in MainActivity.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.res_learn:
textview1settext(#string/str_learn);
return true;
case R.id.res_test:
textview1settext(#string/str_test);
return true;
case R.id.res_result:
textview1settext(#string/str_result);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And these references to strings causes at compilation time the error:
Error:(56, 35) error: type annotations are not supported in -source 1.7
(use -source 8 or higher to enable type annotations)
Now what? I actualized Android Studio within the last few weeks as proposed by the programm. As far as I understand these updates were no actualisation, I loaded down somewhat older.
Or has this error-message another reason. How do I fix this error.
Try to use:
textview1settext(R.string.str_learn);
Btw just try to find some course about android development for beginners.
For example on Coursera or Udacity.

Zxing 2.2 and R.id error

I would like to integrate zxing into my app.
I'm importing project, set it as library, change target to API 7, download zxing-core-2.2.jar, copy it to /libs and add this jar in Java Building Path as library.
But there are still some errors:
All of them are in switch statements and depend on R.id. for example:
switch (item.getItemId()) {
case R.id.menu_share:
Eclipse error description:
case expressions must be constant expressions
there is a info dialog:
Any idea what I'm doing wrong or how to fix it?
As explained in the dialog you have shown, R.id.menu_settings is now "no longer constant", which means it cannot be used in a switch. the dialog also provides the solution, instead of
switch (item.getItemId()) {
case R.id.menu_share:
//do something
break;
case xxx:
...
}
You should do this:
if(item.getItemId()==R.id.menu_share) {
//do something
} else if (item.getItemId()==xxx) {
//do something
}
Just follow the instruction and you should be fine.
The problem is the source of zxing is not intended to be used as a Library..
Please see the answer by Sean in the below thread.
Zxing project as library in a project won't build
I tried bridling my app within this CaptureActivity project adding my activity and resources and modifying its manifest File accordingly.

Categories

Resources