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

时间:2015-04-24 09:04:15

标签: android arrays navigation-drawer

我在找到strings.xml的字符串数组时遇到问题。

错误是:

What went wrong?

基本上我尝试做的是获取一个名为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中。 This is one of google's examples for navigation drawer, and I downloaded it from their site and modified it.

由于某种原因,我在Nexus 4(Android 5.0棒棒糖)上没有收到此错误,但我确实在Galaxy S3和LG G2(android 4.4.4)上遇到了问题

这是我的完整代码:

mPlanetTitles

第87行:

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

的strings.xml:

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

我试图创建一个名为res的资源,而不是使用getResources()而且它没有用。 这就是我的尝试:

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

6 个答案:

答案 0 :(得分:14)

确保您没有values资源的变体,字符串数组仅针对变体的子集定义。例如:

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

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

答案 1 :(得分:6)

我收到此错误是因为我使用TextView.setText(int)在文本中设置了一个数字。问题是Android认为整数是一个ressource id。因此,我应该将String.valueOf(int)内部的整数封装起来,以便代码变为TextView.setText(String.valueOf(int));

答案 2 :(得分:0)

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

答案 3 :(得分:0)

执行此操作以将Integer呈现为textview

TextView.setText(String.valueOf(int));

答案 4 :(得分:0)

我也有同样的错误。 我只是使缓存无效,然后重新启动android studio,然后一切正常。 要使缓存无效并重新启动“转到文件”>“使缓存无效/重新启动”。 这是android studio的某种错误,它在我身上发生过几次。

答案 5 :(得分:-1)

就我而言 array.xml的版本为w820dp。 我刚刚在values.xml中创建了另一个,没有w820dp。 可以!!!