0
String[] string = {getResourse().getString(R.string.girl), getResourse().getString(R.string.boy), getResourse().getString(R.string.child)};// Why is this code not working

And I tried do like this

String[] string = {getString(R.string.girk)....};

And also I tried do like

String[] string = {R.string.girl...};

Everything is not working, or it is impossible?

11
  • Have you tried -> String[] strings = new String[] {getResourse().getString(R.string.girl)}; ? Commented Feb 10, 2017 at 16:40
  • @TiagoDávila Yes, just tried. But This is not the solution of problem Commented Feb 10, 2017 at 16:44
  • @DeveloperChingis then in that case what is the problem?? How you want it Commented Feb 10, 2017 at 16:46
  • @TiagoDávila I have tried you option String[] strings = new String[] {getResourse().getString(R.string.girl)};.But the app crashes Commented Feb 10, 2017 at 16:49
  • @sanjeetkumarSingh I have tried the option String[] strings = new String[] {getResourse().getString(R.string.girl)};.But the app crashes Commented Feb 10, 2017 at 16:50

2 Answers 2

1

First of all, R.string.girl is int type.

If you really want to define it programatically like below,

String[] string2 = {getString(R.string.app_name)};

You should define it when your Activity/fragment's context is avaliable. For example, you can define it inside onCreate method but it is impossible to define it as a global variable (Because getString is the method of Context class).

Sign up to request clarification or add additional context in comments.

Comments

1

You can do like in this example.

enter image description here

For more info https://developer.android.com/guide/topics/resources/string-resource.html#StringArray

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.