1

I'm probably missing something really obvious here. I have created a new string called str but when I try to assign a string value from strings.xml to it I am getting the error "The method getString(int) is undefined for the type String". Not sure why it thinks it is an int?

It works fine if I just set str = "my string"?

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_clinics);

}

public void onRadioButtonClicked(View view) {

    // initialize the string variable
    String str; 

    // Is the button now checked?
    boolean checked = ((RadioButton) view).isChecked();
    // Check which radio button was clicked
    switch (view.getId()) {
    case R.id.radioCounty1:
        if (checked)
            str.getString(R.string.County1);
        break;
    case R.id.radioCounty2:
        if (checked)
            str.getString(R.string.County2);
        break;
    }

5 Answers 5

3

String has no member getString. You are probably looking for Context.getString(int).

here you can find the documentation.

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

2 Comments

Yes this worked. Other answers below were more or less the same too. I just don't get what the point of referring to a context is when I am working within one class. Or more to the point why doesn't String have a getString member??
Context is a class with getString method. Activity extends (in its hierarchy Context), so from an activity you are able to call the getString method. It looks up the R class to get the String you want. String is a java class. It has no notion of Android resources
2

String has no method named getString.

You should use

str = Activity_name.this.getResources().getString(R.string.County1);

Where Activity_name is the name of the current activity

Comments

1

You have to use str = getResources().getString(R.string.County1) instead

Comments

1

pass a instance of Context context

and then use

context.getResources().getString(R.string.text1) here context is belongs to your current activity.

Comments

1

Since String does not have getString() you cannot use it directly with String. Instead You cannot use

str = getResources().getString(R.string.County1);

or

str = Context.getString(R.string.County1);

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.