https://kotlinlang.org logo
#android
Title
# android
l

luke

01/30/2020, 12:27 AM
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

Tim Malseed

01/30/2020, 12:27 AM
What does your code look like?
l

luke

01/30/2020, 12:28 AM
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

Tim Malseed

01/30/2020, 12:29 AM
And what do you mean by ‘I’m unable to get a string’?
l

luke

01/30/2020, 12:30 AM
Sorry, should have been clearer, getting an exception.
t

Tim Malseed

01/30/2020, 12:30 AM
You are calling
getStringArray()
, which returns an
Array<String>
l

luke

01/30/2020, 12:30 AM
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

Tim Malseed

01/30/2020, 12:33 AM
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

luke

01/30/2020, 12:35 AM
That's what I thought too, but it's importing my package's R
t

Tim Malseed

01/30/2020, 12:36 AM
Hmm. I really don’t have any other ideas. So, you’re getting the same problem in a separate project?
l

luke

01/30/2020, 12:36 AM
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

Tim Malseed

01/30/2020, 12:41 AM
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

luke

01/30/2020, 1:27 AM
Yeah, totally at a loss here.
j

Jiri Bruchanov

01/30/2020, 11:38 AM
@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"
3 Views