下面的脚本是SettingsActivity.java
public class SettingsActivity extends AppCompatActivity {
Toolbar toolbar;
Context context;
FragmentActivity activity;
CallbackManager callbackManager;
Button changelanguage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
this.context = this;
//set toolbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// close on back button pressed
finish();
}
});
//set fragment to MyPreferenceFragment
getFragmentManager().beginTransaction().replace(R.id.content, new MyPreferenceFragment()).commit();
loadLocale();
activity = this;
//set change language button
changelanguage = findViewById(R.id.change_lang_btn2);
changelanguage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Array of languages to dispaly in AlertDialog
final String[] listitems ={"English","Spanish | Español","Portuguese | Português","French | français"};
// Create AlertDialog to display list of languages
AlertDialog.Builder alert = new AlertDialog.Builder(SettingsActivity.this);
alert.setTitle("Choose Language..");
alert.setSingleChoiceItems(listitems, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (i == 0){
//set English
setLocale("en");
recreate();
}
else if (i == 1){
//set Spanish
setLocale("es");
recreate();
}
else if (i == 2){
//set Portuguese
setLocale("pt");
recreate();
}
else if (i == 3){
//set french
setLocale("fr");
recreate();
}
//dismiss alert dialog when language is selected
dialogInterface.dismiss();
}
});
alert.show();
}
});
}
private void setLocale(String lang) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());
//save data to sharedpreferences
SharedPreferences.Editor editor = getSharedPreferences("lang_settings",MODE_PRIVATE).edit();
editor.putString("My_lang",lang);
editor.apply();
}
//load language saved in shared preferences
public void loadLocale(){
SharedPreferences prefs = getSharedPreferences("lang_settings", Activity.MODE_PRIVATE);
String language = prefs.getString("My_lang", "");
setLocale(language);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
下面的脚本是Activity_Settings.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"
android:fitsSystemWindows="true"
android:gravity="center"
tools:context="com.riipo.gt.MainActivity">
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<FrameLayout
android:id="@+id/drawer_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar">
<fragment
android:id="@+id/content"
class="com.riipo.helpers.MyPreferenceFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</FrameLayout>
<Button
android:id="@+id/change_lang_btn2"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:layout_margin="10dp"
android:backgroundTint="@color/accent"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="@string/change_language_button"
android:textColor="@color/colorWhite"
android:textSize="14sp"
/>
</RelativeLayout>
This is the error I'm getting:
我现在将描述问题,脚本是通过配置区域内的按钮更改我的应用程序语言的一种方式,这种脚本位于SettingsActivity.java中,单击该按钮后,出现的选项是选择,然后选择应用程序。
上面显示的错误消息应该是一个简单的语言交换系统,但是我遇到了几天,有人可以帮我吗?
提前谢谢!