0

I am getting

Incompatible Types. Required java.lang.String Found int

here is what i am using:

private String[] items = {R.string.name_1, R.string.name_2};

and then i would like to make something like this:

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setItems(items, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                strName = items[which];

                if(strName.equalsIgnoreCase(R.string.name_1))
                {
                    // call activity
                }
                ....
          }

Updated as suggested by @Eran

          private String[] items = {
            getResources().getString(R.string.name_1),
            getResources().getString(R.string.name_2)
          };

          builder.setItems(items, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                strName = items[which];             
                if(strName.equalsIgnoreCase(getResources().getString(R.string.name_1)))
                {
                    // call activity
                }
                .......
            }

Still facing issue as shown below, FYI i am not using English as String

Log says:

Caused by: java.lang.NullPointerException at android.content.ContextWrapper.getResources(ContextWrapper.java:81) at com.lingual.game.MainActivity.<init>(MainActivity.java:46)

1
  • which is line no. 46 in MainActivity ?? does it "if(strName.equalsIgnoreCase(getResources().getString(R.string.name_1)))" Commented Jun 2, 2015 at 6:55

4 Answers 4

8

R.string.name_1 is an int identifier referring to a String resource, to you can't compare it to a String. To get the String it refers to you need to write getResources().getString(R.string.name_1).

Therefore

if(strName.equalsIgnoreCase(R.string.name_1))

should be

if(strName.equalsIgnoreCase(getResources().getString(R.string.name_1)))

Similarly

private String[] items = {R.string.name_1, R.string.name_2};

should be

private String[] items = {getResources().getString(R.string.name_1), getResources().getString(R.string.name_2)};

EDIT:

Based on the following exception you got, it looks like this code appears in the constructor of your activity class, when the activity is not initialized yet. You should move it to onCreate(Bundle savedInstanceState).

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

4 Comments

getting: Caused by: java.lang.NullPointerException at android.content.ContextWrapper.getResources(ContextWrapper.java:81) at com.lingual.game.MainActivity.<init>(MainActivity.java:46) check updated code as suggested by you, i am not using English as String ....
yes Eran i am using see updated code.. but still facing issue
@Sun which line is line 46 is MainActivity? Is it the if statement?
@Sun It looks like this code appears in the constructor of your class, when the Activity is not initialized yet. You should move it to onCreate().
2

R is all integer values. You need to use

getResources().getString(R.string.value);

And if you want to store the integers and retrieve the Strings on the fly, use

private int[] items = {R.string.x ..., R.string.z);

and use

getResources().getString(items[i])

Comments

2

For array of strings, create a string array in strings.xml, for ex:,

<string-array name="my_string_array">
    <item>Name1</item>
    <item>Name2</item>
    <item>Name3</item>        
</string-array>

And then assign it to your variable like this,

String[] items = getResources().getStringArray(R.array.my_string_array);

Comments

1

Using R.string.key returns the resource id from the given key.

You should use

private String[] items = {getResources().getString(R.string.name_1), getResources().getString(R.string.name_2)};

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.