I'm having problem getting a string array for strings.xml .
The error is:
android.content.res.Resources$NotFoundException: String array resource ID #0x7f070002
at android.content.res.Resources.getStringArray(Resources.java:472)
at com.klh.lehava.MainActivity.onCreate(MainActivity.java:87)
Basically what I'm trying to do is to get a string-array called planets_array
, and store it into mPlanetTitles
.
This is one of google's examples for navigation drawer, and I downloaded it from their site and modified it.
For some reason I don't get this error on Nexus 4 (android 5.0 lollipop), but I do get the problem on Galaxy S3, and LG G2 (android 4.4.4)
Here is my full code:
public class MainActivity extends Activity {
private static MainActivity instance;
public MainActivity() {
instance = this;
}
private static String response_str;
private static HttpResponse result;
private DrawerLayout mDrawerLayout;
private static ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private ArrayAdapter<String> adapter1;
private static String isUserLoggedIn;
private static String getUrl;
public String getUrl() {
return getUrl;
}
public static final String TAG = "loginCookie";
private CharSequence mDrawerTitle;
private static CharSequence mTitle;
//private final ArrayList<String> assignArr0 = new ArrayList<String>();
private static String[] mPlanetTitles;
private static String[] mPlanetTitlesDisconnected;
private static String[] mPlanetTitlesConnected;
private static int checked;
private String[] newAr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview;
webview = (WebView) findViewById(R.id.webview);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mPlanetTitlesDisconnected = getResources().getStringArray(R.array.mPlanetTitlesDisconnected);
mPlanetTitlesConnected = getResources().getStringArray(R.array.mPlanetTitlesConnected);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
adapter1 = new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitlesDisconnected);
mDrawerList.setAdapter(adapter1);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch (item.getItemId()) {
default:
return super.onOptionsItemSelected(item);
}
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
Bundle args = new Bundle();
//isUserLoggedIn=IsLoggedIn();
if (isUserLoggedIn == null) {
Fragment fragment = new MainFragment();
args.putInt(MainFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
if (isUserLoggedIn.equals("0")) {
if (position == 0) {
Fragment fragment = new MainFragment();
args.putInt(MainFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
if (position == 1) {
Fragment fragment = new AboutFragment();
args.putInt(AboutFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
} else if (isUserLoggedIn.equals("1")) {
if (position == 0) {
Fragment fragment = new InOutFragment();
args.putInt(InOutFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
if (position == 1) {
Fragment fragment = new AddEventFragment();
args.putInt(AddEventFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
if (position == 2) {
Fragment fragment = new AboutFragment();
args.putInt(AboutFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
if (position == 3) {
Fragment fragment = new WebLogoutFragment();
args.putInt(WebLogoutFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(0, true);
setTitle(mPlanetTitles[0]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
}
// update selected item and title, then close the drawer
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
public static class PlanetFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public PlanetFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", getActivity().getPackageName());
((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
getActivity().setTitle(planet);*/
return null;
}
}
public static class AboutFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public AboutFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_about, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
getActivity().setTitle(planet);
return rootView;
}
}
public static class MainFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public static final String MY_DOMAIN = "klh-dev.com";
public MainFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_web, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
WebView webview;
webview = (WebView) rootView.findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
String weburl = view.getUrl();
String cookies = CookieManager.getInstance().getCookie(url);
getUrl = view.getUrl();
isUserLoggedIn = IsLoggedIn();
Log.d(TAG, "All the cookies in a string:" + cookies);
if (weburl.equals("http://www.klh-dev.com/lehava/lehava/system/mlogin.php")) {
mDrawerList.setItemChecked(0, true);
getActivity().getActionBar().setTitle("התחברות");
} else if (weburl.equals("http://www.klh-dev.com/lehava/lehava/system/mtimecounter.php")) {
mDrawerList.setItemChecked(1, true);
getActivity().getActionBar().setTitle("כניסה/יציאה");
} else if (weburl.equals("http://www.klh-dev.com/lehava/lehava/system/madd.php")) {
mDrawerList.setItemChecked(2, true);
getActivity().getActionBar().setTitle("הוספת אירוע");
}
}
});
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("http://www.klh-dev.com/lehava/lehava/system/mlogin.php");
getActivity().setTitle(planet);
return rootView;
}
}
public static class InOutFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public static final String MY_DOMAIN = "klh-dev.com";
public InOutFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_web, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
WebView webview;
webview = (WebView) rootView.findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
String weburl = view.getUrl();
String cookies = CookieManager.getInstance().getCookie(url);
getUrl = view.getUrl();
isUserLoggedIn = IsLoggedIn();
Log.d(TAG, "All the cookies in a string:" + cookies);
if (weburl.equals("http://www.klh-dev.com/lehava/lehava/system/mlogin.php")) {
mDrawerList.setItemChecked(0, true);
getActivity().getActionBar().setTitle("התחברות");
} else if (weburl.equals("http://www.klh-dev.com/lehava/lehava/system/mtimecounter.php")) {
mDrawerList.setItemChecked(1, true);
getActivity().getActionBar().setTitle("כניסה/יציאה");
} else if (weburl.equals("http://www.klh-dev.com/lehava/lehava/system/madd.php")) {
mDrawerList.setItemChecked(2, true);
getActivity().getActionBar().setTitle("הוספת אירוע");
}
}
});
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("http://www.klh-dev.com/lehava/lehava/system/mtimecounter.php");
getActivity().setTitle(planet);
return rootView;
}
}
public static class AddEventFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public static final String MY_DOMAIN = "klh-dev.com";
public AddEventFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_web, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
WebView webview;
webview = (WebView) rootView.findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
String weburl = view.getUrl();
String cookies = CookieManager.getInstance().getCookie(url);
getUrl = view.getUrl();
isUserLoggedIn = IsLoggedIn();
Log.d(TAG, "All the cookies in a string:" + cookies);
if (weburl.equals("http://www.klh-dev.com/lehava/lehava/system/mlogin.php")) {
mDrawerList.setItemChecked(0, true);
getActivity().getActionBar().setTitle("התחברות");
} else if (weburl.equals("http://www.klh-dev.com/lehava/lehava/system/mtimecounter.php")) {
mDrawerList.setItemChecked(1, true);
getActivity().getActionBar().setTitle("כניסה/יציאה");
} else if (weburl.equals("http://www.klh-dev.com/lehava/lehava/system/madd.php")) {
mDrawerList.setItemChecked(2, true);
getActivity().getActionBar().setTitle("הוספת אירוע");
}
}
});
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("http://www.klh-dev.com/lehava/lehava/system/madd.php");
getActivity().setTitle(planet);
return rootView;
}
}
public static class WebLogoutFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public WebLogoutFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_web, container, false);
WebView webview;
webview = (WebView) rootView.findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
String weburl = view.getUrl();
String cookies = CookieManager.getInstance().getCookie(url);
getUrl = view.getUrl();
isUserLoggedIn = IsLoggedIn();
Log.d(TAG, "All the cookies in a string:" + cookies);
if (weburl.equals("http://www.klh-dev.com/lehava/lehava/system/mlogin.php")) {
mDrawerList.setItemChecked(0, true);
getActivity().getActionBar().setTitle("התחברות");
} else if (weburl.equals("http://www.klh-dev.com/lehava/lehava/system/mlogin.php?log=1")) {
mDrawerList.setItemChecked(0, true);
getActivity().getActionBar().setTitle("התחברות");
} else if (weburl.equals("http://www.klh-dev.com/lehava/lehava/system/mtimecounter.php")) {
mDrawerList.setItemChecked(1, true);
getActivity().getActionBar().setTitle("כניסה/יציאה");
} else if (weburl.equals("http://www.klh-dev.com/lehava/lehava/system/madd.php")) {
mDrawerList.setItemChecked(2, true);
getActivity().getActionBar().setTitle("הוספת אירוע");
} else if (weburl.equals("http://www.klh-dev.com/lehava/lehava/system/mlogin.php?log=1")) {
mDrawerList.setItemChecked(2, true);
getActivity().getActionBar().setTitle("התחברות");
}
}
});
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setUseWideViewPort(true);
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("http://www.klh-dev.com/lehava/lehava/system/mlogout.php");
return rootView;
}
}
public static BasicCookieStore getCookieStore(String cookies, String domain) {
String[] cookieValues = cookies.split(";");
BasicCookieStore cs = new BasicCookieStore();
BasicClientCookie cookie;
for (int i = 0; i < cookieValues.length; i++) {
String[] split = cookieValues[i].split("=");
if (split.length == 2)
cookie = new BasicClientCookie(split[0], split[1]);
else
cookie = new BasicClientCookie(split[0], null);
cookie.setDomain(domain);
cs.addCookie(cookie);
}
return cs;
}
public void UpdateMenu()
{
runOnUiThread(new Runnable()
{
public void run()
{
if (isUserLoggedIn == null) {
isUserLoggedIn = IsLoggedIn();
}
int i;
SparseBooleanArray localSparseBooleanArray;
if (isUserLoggedIn.equals("0")) {
mPlanetTitles = mPlanetTitlesDisconnected;
}
i = mDrawerList.getCount();
localSparseBooleanArray = mDrawerList.getCheckedItemPositions();
for (int j = 0; j < i ; j++)
{
if (isUserLoggedIn.equals("1")) {
mPlanetTitles = mPlanetTitlesConnected;
}
mDrawerList.setAdapter(new ArrayAdapter(instance, R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setItemChecked(checked, true);
if (localSparseBooleanArray.get(j)) {
checked = j;
}
}
}
});
}
public void showToast(final String toast)
{
runOnUiThread(new Runnable() {
public void run()
{
Toast.makeText(instance, toast, Toast.LENGTH_SHORT).show();
}
});
}
public static String IsLoggedIn()
{
new Thread(new Runnable()
{
public void run()
{
BasicCookieStore localBasicCookieStore = getCookieStore(CookieManager.getInstance().getCookie(getUrl), "klh-dev.com");
BasicHttpContext localBasicHttpContext = new BasicHttpContext();
DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient();
localDefaultHttpClient.setCookieStore(localBasicCookieStore);
localBasicHttpContext.setAttribute("http.cookie-store", localBasicCookieStore);
HttpGet localHttpGet = new HttpGet("http://klh-dev.com/lehava/lehava/system/isloggedin.php");
try
{
result = localDefaultHttpClient.execute(localHttpGet, localBasicHttpContext);
response_str = EntityUtils.toString(result.getEntity());
System.out.println(response_str);
instance.UpdateMenu();
isUserLoggedIn = response_str;
}
catch (ClientProtocolException localClientProtocolException)
{
localClientProtocolException.printStackTrace();
}
catch (IOException localIOException)
{
localIOException.printStackTrace();
}
}
}).start();
String str = response_str;
isUserLoggedIn = str;
return str;
}
}
Line 87:
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
strings.xml:
<resources>
<string name="app_name">Lehava</string>
<string-array name="planets_array">
<item>login</item>
<item>about</item>
<item>start shift</item>
<item>add event</item>
<item>logout</item>
</string-array>
<string-array name="mPlanetTitlesDisconnected">
<item>login</item>
<item>about</item>
</string-array>
<string-array name="mPlanetTitlesConnected">
<item>start shift</item>
<item>add event</item>
<item>about</item>
<item>logout</item>
</string-array>
<string name="drawer_open">Open navigation drawer</string>
<string name="drawer_close">Close navigation drawer</string>
<string name="action_websearch">Web search</string>
</resources>
I have tried to create a resource named res and use getResources() instead and it didn't work. this is what I have tried:
Resources res = getResources();
mPlanetTitles = res.getStringArray(R.array.planets_array);
Make sure you don't have variants of the values
resources, with the string array defined only for a subset of the variants. For example:
values-v21/strings.xml - defined
values/strings.xml - not defined
If the resource is present in at least one variant, you get the entry in R
and can refer to it in code, but the runtime resource file variant does not necessarily have the resource.
I got this error because I used TextView.setText(int)
to set a number in the text. The problem is that Android thinks that the integer is a ressource id. Therefore I should encapsulate the interger inside of String.valueOf(int)
so the code becomes TextView.setText(String.valueOf(int));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With