Android中的区域设置更改后应用程序无法查找资源

时间:2015-01-21 16:12:51

标签: android crash resources locale

我正在开发一个需要即时更改语言的项目。我正在尝试设置所需的区域设置,但是一旦我这样做,应用程序崩溃抱怨它无法找到资源(像R.layout。* R.drawable。*)

这是我用来设置新语言环境的代码:

            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());

编辑:添加日志

E / AndroidRuntime:致命异常:主要     java.lang.RuntimeException:无法启动活动ComponentInfo {com.flavorburst.mcdonalds / com.flavorburst.mcdonalds.MyActivity}:android.view.InflateException:二进制XML文件行#25:错误膨胀类片段             在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)             在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)             在android.app.ActivityThread.access $ 600(ActivityThread.java:141)             在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)             在android.os.Handler.dispatchMessage(Handler.java:99)             在android.os.Looper.loop(Looper.java:137)             在android.app.ActivityThread.main(ActivityThread.java:5041)             at java.lang.reflect.Method.invokeNative(Native Method)             在java.lang.reflect.Method.invoke(Method.java:511)             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)             at dalvik.system.NativeStart.main(Native Method)      引起:android.view.InflateException:二进制XML文件行#25:错误膨胀类片段             在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)             在android.view.LayoutInflater.rInflate(LayoutInflater.java:746)             在android.view.LayoutInflater.inflate(LayoutInflater.java:489)             在android.view.LayoutInflater.inflate(LayoutInflater.java:396)             在android.view.LayoutInflater.inflate(LayoutInflater.java:352)             在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)             在android.app.Activity.setContentView(Activity.java:1881)             在com.flavorburst.mcdonalds.MyActivity.onCreate(MyActivity.java:106)             在android.app.Activity.performCreate(Activity.java:5104)             在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)             在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)             在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)             在android.app.ActivityThread.access $ 600(ActivityThread.java:141)             在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)             在android.os.Handler.dispatchMessage(Handler.java:99)             在android.os.Looper.loop(Looper.java:137)             在android.app.ActivityThread.main(ActivityThread.java:5041)             at java.lang.reflect.Method.invokeNative(Native Method)             在java.lang.reflect.Method.invoke(Method.java:511)             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)             at dalvik.system.NativeStart.main(Native Method)      引起:android.content.res.Resources $ NotFoundException:字符串数组资源ID#0x7f08001e             在android.content.res.Resources.getStringArray(Resources.java:451)             at com.flavorburst.mcdonalds.NavigationDrawerFragment.onCreate(NavigationDrawerFragment.java:91)             在android.app.Fragment.performCreate(Fragment.java:1673)             在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:854)             在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)             在android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)             在android.app.Activity.onCreateView(Activity.java:4717)             在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)             在android.view.LayoutInflater.rInflate(LayoutInflater.java:746)             在android.view.LayoutInflater.inflate(LayoutInflater.java:489)             在android.view.LayoutInflater.inflate(LayoutInflater.java:396)             在android.view.LayoutInflater.inflate(LayoutInflater.java:352)             在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)             在android.app.Activity.setContentView(Activity.java:1881)             在com.flavorburst.application.MyActivity.onCreate(MyActivity.java:106)             在android.app.Activity.performCreate(Activity.java:5104)             在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)             在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)             在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)             在android.app.ActivityThread.access $ 600(ActivityThread.java:141)             在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)             在android.os.Handler.dispatchMessage(Handler.java:99)             在android.os.Looper.loop(Looper.java:137)             在android.app.ActivityThread.main(ActivityThread.java:5041)             at java.lang.reflect.Method.invokeNative(Native Method)             在java.lang.reflect.Method.invoke(Method.java:511)             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)             在dalvik.system.NativeStart.main(本地方法)

2 个答案:

答案 0 :(得分:0)

您是否尝试过清理并重建项目?如果你正在使用eclipse得到Project - >清洁。然后重新构建。如果您使用的是Android Studio和gradle,请在终端上键入以下命令行:

./gradlew clean 

使用Build - >重建项目

答案 1 :(得分:0)

此错误:

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

是指您在其中一个XML文件中定义的字符串数组,但您忘记在/values/array.xml中创建字符串数组值。例如,在您的一个XML文件中,您可能指定了一个字符串数组值" arrayNames",但您忘记在/values/array.xml中声明它:

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

因此,在运行时,编译器会抱怨它无法找到&#34; arrayNames&#34;在/values/array.xml。因此,您运行应用程序时出现的错误。