1
votes

I am working on a project which requires on the fly language changes. I am attempting to set the desired Locale but as soon as I do, the app crashes complaining that it cannot find Resources (things like R.layout.* R.drawable.*)

This is the code I am using to set the new Locale:

            String languageToLoad = "es";
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());

Edit: added log

E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.flavorburst.mcdonalds/com.flavorburst.mcdonalds.MyActivity}: android.view.InflateException: Binary XML file line #25: Error inflating class fragment at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: android.view.InflateException: Binary XML file line #25: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java:396) at android.view.LayoutInflater.inflate(LayoutInflater.java:352) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270) at android.app.Activity.setContentView(Activity.java:1881) at com.flavorburst.mcdonalds.MyActivity.onCreate(MyActivity.java:106) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) Caused by: android.content.res.Resources$NotFoundException: String array resource ID #0x7f08001e at android.content.res.Resources.getStringArray(Resources.java:451) at com.flavorburst.mcdonalds.NavigationDrawerFragment.onCreate(NavigationDrawerFragment.java:91) at android.app.Fragment.performCreate(Fragment.java:1673) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:854) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035) at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137) at android.app.Activity.onCreateView(Activity.java:4717) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680) at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java:396) at android.view.LayoutInflater.inflate(LayoutInflater.java:352) at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270) at android.app.Activity.setContentView(Activity.java:1881) at com.flavorburst.application.MyActivity.onCreate(MyActivity.java:106) at android.app.Activity.performCreate(Activity.java:5104) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) at android.app.ActivityThread.access$600(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method)

2
Post you stacktrace from LogCat.ChuongPham
As per your log, it looks like you have a problem with one or more of your resource files (strings.xml) at line 25. Check this line.ChuongPham
I think that line 25 is referring to the layout it is trying to inflate, not the strings file.Daniel Jackson

2 Answers

0
votes

This error:

Caused by: android.content.res.Resources$NotFoundException: String array resource ID #0x7f08001e at android.content.res.Resources.getStringArray(Resources.java:451) at com.flavorburst.mcdonalds.NavigationDrawerFragment

refers to a string array that you have defined in one of your XML files, but you forgot to create the string array value in /values/array.xml. For example, in one of your XML files, you may have specified a string array value of "arrayNames", but you forgot to declare it in /values/array.xml like:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="arrayNames"/>
</resources>

So, at runtime, the compiler will complain that it cannot find "arrayNames" in /values/array.xml. Hence, the error you got when you run your app.

0
votes

Have you tried to clean and rebuild your project? If you are using eclipse got to Project -> Clean. And then build again. If you are using Android Studio and gradle, type the following command line on a terminal:

./gradlew clean 

or

use the Build -> Rebuild Project