android.content.res.Resources $ NotFoundException:string数组资源ID#0x7f070002

我有问题获取strings.xml的string数组

错误是:

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) 

基本上我想要做的是得到一个名为planets_array的string数组,并将其存储到mPlanetTitles 。 这是谷歌的导航抽屉的例子之一,我从他们的网站下载并修改它。

由于某种原因,我不会在Nexus 4(android 5.0棒棒糖)上出现这个错误,但我确实遇到了Galaxy S3和LG G2(android 4.4.4)

这是我的完整代码:

 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; } } 

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> 

我试图创build一个名为res的资源,并使用getResources(),而不是工作。 这是我曾经试过的:

 Resources res = getResources(); mPlanetTitles = res.getStringArray(R.array.planets_array); 

Solutions Collecting From Web of "android.content.res.Resources $ NotFoundException:string数组资源ID#0x7f070002"

确保没有values资源的变体,string数组仅为变体的一个子集定义。 例如:

 values-v21/strings.xml - defined values/strings.xml - not defined 

如果资源至less存在一个变体,则您在R获得条目,并可以在代码中引用该条目,但运行时资源文件变体不一定具有该资源。

我得到这个错误,因为我用TextView.setText(int)在文本中设置一个数字。 问题是,Android认为整数是一个资源ID。 因此,我应该封装String.valueOf(int)的整数,所以代码变成TextView.setText(String.valueOf(int));

我解决了这个问题。 Juste创build一个values / strings.xml文件,其中的所有string值都在values-v21 / strings.xml中定义。