我正在通过一个应用程序实现本地化示例。我使用应用程序类将所选语言更新为整个应用程序。问题是当我第一次更改语言时,它会更改为整个应用程序。同样,当我尝试更改语言时,它会给出强制关闭错误。错误出现在这一行 editor.putString("Language", getString(identifier)); 下面是我的代码和 logcat 错误。我不明白如何解决这个问题。
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
TextView mTextView = (TextView) v.findViewById(android.R.id.text1);
Toast.makeText(getActivity(), mTextView.getText().toString(), Toast.LENGTH_SHORT).show();
int identifier = getResources().getIdentifier(
mTextView.getText().toString(), "string",
getActivity().getPackageName());
String language = getString(identifier);
Toast.makeText(getActivity().getApplicationContext(), language, Toast.LENGTH_SHORT).show();
editor.clear();
editor.putString("Language", getString(identifier));
editor.commit();
changeLanguage(getString(identifier));
} /**
* Method to change the language
*
* @param localString
*/
public void changeLanguage(String localString)
{
AllUpdateApplication.updateLanguage(getActivity().getApplicationContext());
}
public class AllUpdateApplication extends Application{
/**
* Application wide language change
* @param mContext
*/
public static void updateLanguage( Context mContext){
SharedPreferences mUWBTPRefs = mContext.getSharedPreferences("language", 0);
String mLanguage = mUWBTPRefs.getString("Language", "en");
//Toast.makeText(mContext,mLanguage, Toast.LENGTH_SHORT).show();
Configuration mConfiguration = new Configuration();
mConfiguration.locale = new Locale(mLanguage);
mContext.getResources().updateConfiguration(mConfiguration, null);
}
}
02-18 15:30:28.660: E/AndroidRuntime(6819): FATAL EXCEPTION: main
02-18 15:30:28.660: E/AndroidRuntime(6819): android.content.res.Resources$NotFoundException: String resource ID #0x0
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.content.res.Resources.getText(Resources.java:247)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.content.res.Resources.getString(Resources.java:331)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.support.v4.app.Fragment.getString(Fragment.java:593)
02-18 15:30:28.660: E/AndroidRuntime(6819): at com.uwbt.fragments.LanguagesFragment.onListItemClick(LanguagesFragment.java:89)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3180)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.View.dispatchTouchEvent(View.java:5545)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1951)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1712)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1957)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1726)
02-18 15:30:28.660: E/AndroidRuntime(6819): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1912)
02-18 15:30:28.660: E/AndroidRuntime(6819): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.app.Activity.dispatchTouchEvent(Activity.java:2391)
02-18 15:30:28.660: E/AndroidRuntime(6819): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1860)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.View.dispatchPointerEvent(View.java:5725)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2928)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2492)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:870)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2501)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.os.Looper.loop(Looper.java:137)
02-18 15:30:28.660: E/AndroidRuntime(6819): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-18 15:30:28.660: E/AndroidRuntime(6819): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 15:30:28.660: E/AndroidRuntime(6819): at java.lang.reflect.Method.invoke(Method.java:511)
02-18 15:30:28.660: E/AndroidRuntime(6819): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-18 15:30:28.660: E/AndroidRuntime(6819): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-18 15:30:28.660: E/AndroidRuntime(6819): at dalvik.system.NativeStart.main(Native Method)