Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with strings.xml... can't pass R.string.foo as a CharSequence

I'm running a TabActivity. In the following line:

spec = tabHost.newTabSpec("alltime").setIndicator(R.string.plots_allTime)
       .setContent(intent);

I get an error because setIndicator() expects a CharSequence. I'm not really sure how to fix this, because I should be able to pass a string into that parameter. I think the issue lies in the fact that the generated R.java initializes everything in the strings.xml file as public static final int. The setIndicator() method doesn't seem to like that too much. Is there any way around this?

like image 625
dfetter88 Avatar asked Nov 06 '10 19:11

dfetter88


2 Answers

spec = tabHost.newTabSpec("alltime").setIndicator(getString(R.string.plots_allTime))
.setContent(intent);
like image 161
Peter Knego Avatar answered Oct 20 '22 04:10

Peter Knego


You need to get a string corresponding to the ID from R.string: use context.getText, which returns a localized, styled CharSequence from the application's package's default string table:

setIndicator(context.getText(R.string.plots_allTime) )
like image 42
Asahi Avatar answered Oct 20 '22 04:10

Asahi