user3626123
user3626123

Reputation:

having trouble getting a stringArray resource with a string variable

i am trying to access string array from resource by using variable string in my code... here is my code

package com.example.tryapp_5;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String group="SCIENCE";

    int holderint = getResources().getIdentifier(group, "Array",
            MainActivity.this.getPackageName());

    String[] items = getResources().getStringArray(holderint);

    Log.d("DEBUG", items[0]);
    } 

    }

and here is my xml code....

 <string-array name="SCIENCE">
    <item>BENGALI</item>
    <item>ENGLISH</item>
    <item>MATHMETICS</item>
    <item>PHYSICS</item>
    <item>CHEMISTRY</item>
    <item>BIOLOGY</item>
</string-array>

i don't what is the wrong. every time it crashed with logcat

android.content.res.Resources$NotFoundException: String array resource ID #0x0  

thanks in advance.

Upvotes: 0

Views: 53

Answers (1)

AADProgramming
AADProgramming

Reputation: 6335

Try Below code:

Resources res = getResources();
String[] items = res.getStringArray(R.array.SCIENCE);

This code will work when you have XML file saved at res/values/strings.xml

Upvotes: 1

Related Questions