告诉我 - 我想在一个单独的类中读取DefaultSharedPreferences设置。并且已经有其他Activity从中读取数据(在任何Activity数据中都会被读取,但是想要通过一个单独的类来实现它,每个Activity都不会在每个Activity中读取它们)。 代码 - Main.java
public class Main extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Test t = new Test();
String z = t.getAa();
Toast.makeText(Main.this, z, Toast.LENGTH_LONG).show();
}
}
代码 - Test.java
public class Test extends PreferenceActivity {
private String aa;
private String EdittextListPreferenceFont;
public String getAa() {
addPreferencesFromResource(R.xml.preferences);
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
EdittextListPreferenceFont = prefs.getString("edittext_font",
getString(R.string.font_default));
aa = EdittextListPreferenceFont;
return aa;
}
}
LogCat eclipse
12-29 19:16:13.016: E/AndroidRuntime(5804): FATAL EXCEPTION: main
12-29 19:16:13.016: E/AndroidRuntime(5804): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.game.invasion/com.game.invasion.Main}: java.lang.RuntimeException: This should be called after super.onCreate.
12-29 19:16:13.016: E/AndroidRuntime(5804): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
12-29 19:16:13.016: E/AndroidRuntime(5804): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
12-29 19:16:13.016: E/AndroidRuntime(5804): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
12-29 19:16:13.016: E/AndroidRuntime(5804): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
12-29 19:16:13.016: E/AndroidRuntime(5804): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 19:16:13.016: E/AndroidRuntime(5804): at android.os.Looper.loop(Looper.java:130)
12-29 19:16:13.016: E/AndroidRuntime(5804): at android.app.ActivityThread.main(ActivityThread.java:3835)
12-29 19:16:13.016: E/AndroidRuntime(5804): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 19:16:13.016: E/AndroidRuntime(5804): at java.lang.reflect.Method.invoke(Method.java:507)
12-29 19:16:13.016: E/AndroidRuntime(5804): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
12-29 19:16:13.016: E/AndroidRuntime(5804): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
12-29 19:16:13.016: E/AndroidRuntime(5804): at dalvik.system.NativeStart.main(Native Method)
12-29 19:16:13.016: E/AndroidRuntime(5804): Caused by: java.lang.RuntimeException: This should be called after super.onCreate.
12-29 19:16:13.016: E/AndroidRuntime(5804): at android.preference.PreferenceActivity.requirePreferenceManager(PreferenceActivity.java:212)
12-29 19:16:13.016: E/AndroidRuntime(5804): at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:260)
12-29 19:16:13.016: E/AndroidRuntime(5804): at com.game.invasion.Test.getAa(Test.java:19)
12-29 19:16:13.016: E/AndroidRuntime(5804): at com.game.invasion.Main.onCreate(Main.java:45)
12-29 19:16:13.016: E/AndroidRuntime(5804): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-29 19:16:13.016: E/AndroidRuntime(5804): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
我将很感激帮助或链接在哪里阅读它。 PS - 对不起,谷歌翻译
答案 0 :(得分:0)
我不确定,你想要实现的目标。
但是,PreferenceActivity是一个实现UI以与首选项交互的类。以下是说明:http://developer.android.com/reference/android/preference/PreferenceActivity.html
特别是不要使用构造函数来创建一个,但start就像使用intent的任何其他活动一样。
如果您只需要访问存储的共享首选项值,则无需子类化任何特殊类:
public class PreferenceProxy {
private final SharedPreferences prefs;
public PreferenceProxy(final Context ctx) {
this. prefs = PreferenceManager.getDefaultSharedPreferences(ctx));
}
public String getAa() {
prefs.getString("edittext_font", null); // or any other default value, I did not understand your code snippet
}
}
但是,我怀疑代理是值得的。我建议创建一个界面来定义键,比如“edittext_font”。这降低了密钥中拼写错误的风险。此外,您可能希望只有一个点来获取首选项,例如您的应用程序对象或主活动中的静态方法接受Context(如示例中的构造函数)。但是,不要为每个首选项实现一个方法,除非你因为复杂的默认值管理或其他原因从中获得了某些东西。