Issue
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.
Solution
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
Answered By - AADProgramming
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.