我想知道当我的应用启动/恢复时如何从首选项活动中获取数据。 我有一个偏好活动,用户可以选择应用程序的区域设置,问题似乎是当应用程序启动时,它有时无法读取主要活动的首选项,这会导致强制关闭。这是我的一些代码。 请帮忙,谢谢
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
String ListPreference;
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// Locale
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
private void getPrefs() {
// Get the xml/preferences.xml preferences
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
ListPreference = prefs.getString("listPref", "French");
if (ListPreference.equals("1")) {//do something
String languageToLoad = "fr";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
if (ListPreference.equals("2")) {//do something
String languageToLoad = "en";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// END locale Code
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
screenOn();
setup();
getPrefs();
PreferenceManager.setDefaultValues(this, R.xml.preference, false);
this.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}
private void setup() {
text1= (TextView) findViewById(R.id.Main_Action_Bar_Text);
Button1= (ImageButton) findViewById(R.id.sample);
Button2 = (ImageButton) findViewById(R.id.sample);
Button3 = (ImageButton) findViewById(R.id.sample);
Button4 = (ImageButton) findViewById(R.id.sample);
text2= (TextView) findViewById(R.id.sample);
text3= (TextView) findViewById(R.id.sample);
text4= (TextView) findViewById(R.id.sample);
text5= (TextView) findViewById(R.id.sample);
Typeface font = Typeface.createFromAsset(getAssets(), "font/kb.ttf");
}
@Override
protected void onResume() {
super.onResume();
getPrefs();
setup();
}
}
这是res / xml文件夹中的preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Language">
<ListPreference
android:title="Select Language"
android:key="listPref"
android:defaultValue="1"
android:entries="@array/Language"
android:entryValues="@array/listValues" />
</PreferenceCategory>
</PreferenceScreen>
错误日志
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.information.Main}:
android.view.InflateException: Binary XML file line #5: Error inflating class java.lang.reflect.Constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class java.lang.reflect.Constructor
at android.preference.GenericInflater.createItem(GenericInflater.java:397)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
at android.preference.GenericInflater.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:444)
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:422)
at com.myapp.information.Main.onCreate(Main.java:168)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.preference.GenericInflater.createItem(GenericInflater.java:383)
... 23 more
Caused by: java.lang.NullPointerException
at android.content.res.AssetManager.getResourceTextArray(AssetManager.java:214)
at android.content.res.Resources.getTextArray(Resources.java:361)
at android.content.res.TypedArray.getTextArray(TypedArray.java:628)
at android.preference.ListPreference.<init>(ListPreference.java:49)
... 26 more
// this is the line that throws exception (Main.java:168)
// I assume this line is to read the preferences when app starts
PreferenceManager.setDefaultValues(this, R.xml.preference, false);
再次感谢您抽出宝贵时间。
答案 0 :(得分:0)
你可以在onResume中检查首选项,你不需要把它放在onCreate中。你应该再看看android生命周期。这是非常基础的。 http://developer.android.com/reference/android/app/Activity.html#onResume()