0

I posted my first app and rarely it crashes. I get Resources$NotFoundException on com.github.clans.fab.FloatingActionMenu.setMenuButtonColorNormalResId. And this is how I use it in my onCreate of Not-MainActivity:

fabMenu.setMenuButtonColorNormalResId(MainActivity.fabColor);

fabColor is public static int, Im getting it from Shared preferences like this:

fabColor = mSharedPreferences.getInt(KEY_FAB_COLOR, R.color.colorAccent);

which I set in my ThemeActivity

Nemanja
  • 211
  • 6
  • 16

1 Answers1

0

You should save your colours in resources res/values/colors.xml file and the get them anywhere in the project by calling getResources().getColor(colorInt). More information here: https://developer.android.com/guide/topics/resources/more-resources#Color It is not a good practice to save colors in SharedPreferences in Android.

Angelina
  • 1,473
  • 2
  • 16
  • 34
  • Sorry but your answer has nothing to do with question. I know everything you wrote, and yes, Im keeping my colors in res/values/colors, but in my app user can change colors and that color value needs to be stored in SharedPrefs for next time users starts the app. – Nemanja Sep 14 '18 at 14:02
  • Then I still would not store integer colour values in SharedPreferences. I would store colour key as a `String` there. Then in the method which needs to set the colour I would map this key to the corresponding colour from the colors.xml – Angelina Sep 14 '18 at 14:06
  • I'm not storing integer color values, its integer color resource id! And using `String` and then using `int` corresponding to that `String` is one unnecessary step. Method `.setMenuButtonColorNormalResId` needs an `int`, and... The question here isn't about storing colors to SP, its about `Resources$NotFoundException` that occur in some cases and why? – Nemanja Sep 14 '18 at 14:22
  • check this answer https://stackoverflow.com/questions/6517151/how-does-the-mapping-between-android-resources-and-resources-id-work the resource Ids are generated and can change.. that's why it is better idea not to rely on them and not to store them in SP ;) – Angelina Sep 14 '18 at 14:37