我在eclipse上有一个android项目,所以我为它做了本地化(对于阿拉伯语和英语用户)。
问题只是当我按下按钮(英语或阿拉伯语按钮)来更改当前活动(MainActivity)以外的活动上的按钮文本时,我收到错误: -
不幸的是,appname已停止。
但是当我在不更改这些按钮的文本的情况下运行应用程序时(仅将MainActivity按钮的文本从语言更改为语言)没有错误,应用程序正常工作。
这是 MainActivity.java (注意我把ZAD_ISLAMI_button作为ZAD_ISLAMI_BUTTON的一个实例,它本身就是来自另一个活动的按钮)。
public class MainActivity extends Activity
{
private Button English,Arabic,life_pls,Top_Team ,exit,MobiLink_button,
Amro_Khaled ,Short_SMS_button ,ZAD_ISLAMI_button;
private Locale myLocale;
public void changeLang(String lang)
{
if (lang.equalsIgnoreCase(""))
return;
myLocale = new Locale(lang);
saveLocale(lang);
Locale.setDefault(myLocale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = myLocale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
updateTexts();
}
public void saveLocale(String lang)
{
String langPref = "Language";
SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(langPref, lang);
editor.commit();
}
public void loadLocale()
{
String langPref = "Language";
SharedPreferences prefs = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
String language = prefs.getString(langPref, "");
changeLang(language);
}
private void updateTexts()
{
English.setText(R.string.English);
Arabic.setText(R.string.Arabnas);
life_pls.setText(R.string.Life_Plus_Button);
Top_Team.setText(R.string.Top_Team_Button);
Zad_button.setText(R.string.Zad_Button);
Amro_Khaled.setText(R.string.Amro_Khaled_Button);
Short_SMS_button.setText(R.string.Short_SMS_Button);
//The problem here in ZAD_ISLAMI_button.
ZAD_ISLAMI_button.setText(R.string.ZAD_ISLAMI_BUTTON);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.English= (Button)findViewById(R.id.English);
this.Arabic = (Button)findViewById(R.id.Arabics);
this.life_pls = (Button)findViewById(R.id.Life_Plus_Button);
this.Top_Team =(Button)findViewById(R.id.Top_Team_Button);
this.Amro_Khaled =(Button)findViewById(R.id.Amro_Khaled_Button);
this.ZAD_ISLAMI_button=(Button)findViewById(R.id.ZAD_ISLAMI_BUTTON);
Button english = (Button) findViewById(R.id.English);
english.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String lang = "en";
switch (v.getId()) {
case R.id.English:
lang = "en";
break;
case R.id.Arabics:
lang = "ar";
break;
default:
break;
}
changeLang(lang);
}
});
Button Arabic= (Button) findViewById(R.id.Arabics);
Arabic.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String lang = "ar";
switch (v.getId()) {
case R.id.English:
lang = "en";
break;
case R.id.Arabics:
lang = "ar";
break;
default:
break;
}
changeLang(lang);
}
});
}
}
activity_main.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/MobiLink_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/Short_SMS_Button "
android:text="@string/MobiLink_Button" />
<Button
android:id="@+id/Short_SMS_Button "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/Amro_Khaled_Button"
android:text="@string/Short_SMS_Button" />
<Button
android:id="@+id/Zad_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Amro_Khaled_Button"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="@string/Zad_Button" />
<Button
android:id="@+id/Top_Team_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Zad_Button"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="@string/Top_Team_Button" />
<Button
android:id="@+id/Life_Plus_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/Top_Team_Button"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="@string/Life_Plus_Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/application_title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/English"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="بالانجليزي" />
<Button
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/English"
android:text="@string/exit" />
<Button
android:id="@+id/Arabics"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="بالعربية" />
<Button
android:id="@+id/Amro_Khaled_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="@string/Amro_Khaled_Button" />
</RelativeLayout>