For some reason in my `Service` I'm unable to get ...
# android
l
For some reason in my
Service
I'm unable to get a
String
from resources, tried in
onStartCommand()
onCreate()
and
onDestroy()
, does anyone have any ideas?
t
What does your code look like?
l
Copy code
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {     this.resources.getStringArray(R.array.permissions)
return START_NOT_STICKY
}
I've even tried gettting
R.string.app_name
and that fails too.
t
And what do you mean by ‘I’m unable to get a string’?
l
Sorry, should have been clearer, getting an exception.
t
You are calling
getStringArray()
, which returns an
Array<String>
l
image.png
Strange thing is, I've created a separate project and still having this issue.
Yeah, my original implementation was the array, but I simplified it for just a string and having the same issue.
Tried cleaning, invalidating cache, uninstalling.
OK, so when I use the int values, it seems to work:
t
I wonder if it’s using the wrong
R.*
import
Look at your imports, when you use
R.string.app_name
, is it importing
com.mypackage.R
?
Or is it importing
android.R
?
l
That's what I thought too, but it's importing my package's R
t
Hmm. I really don’t have any other ideas. So, you’re getting the same problem in a separate project?
l
Yeah.
What's interesting here though is that the Resource ID in the evaluator is different fom the generated R
I'm going to restart the computer, just to be safe.
t
if
context.resources.getString(R.string.app_name)
fails in a fresh project, then I would say there’s something wrong with the IDE or gradle.
l
Yeah, totally at a loss here.
j
@luke already seen this issue, in my case it was because if different value of R.id.myvalue in runtime and in evaluate window. Not sure why is this happening, but I guess it's related with AGP resource merging (in case multimodule project) and AS evaluate window sees your module resId value instead of the app's one • in my case the code was working fine, only the eval window was "crashing"