好的,所以下面这段代码放在 OnResume 中,似乎会导致强制关闭。还有另一个活动(选项),它在数据库中设置所有变量,如主题。我想做的就是从数据库 onResume 中恢复主题和其他参数,因此使用 SetThemeLight 等公共空白更改颜色。我只是希望这件事能立即工作,并认为 onResume 是最好的方法。
int Theme = 1;
Theme = prefs.getInt("theme", Theme);
switch(Theme){
case 1:
SetThemeLight (this);
break;
case 2:
SetThemeBlue (this);
break;
case 3:
SetThemeDark (this);
break;
}
并以其中一种SetTheme...
方法为例:
public void SetThemeLight (MyActivity myActivity) {
final EditText edit_text = (EditText) findViewById(R.id.editText1);
edit_text.setBackgroundColor(Color.GRAY);
}
以下是如何访问选项:
public boolean onKeyDown(int keyCode, KeyEvent event) {
//options on 'menu' keypress.
if (keyCode == KeyEvent.KEYCODE_MENU) {
Intent opt = new Intent(this, OptionsActivity.class);
startActivity (opt);
};
return true;
调试信息如下:
12-23 15:37:59.960: E/AndroidRuntime(279): FATAL EXCEPTION: main
12-23 15:37:59.960: E/AndroidRuntime(279): java.lang.RuntimeException: Unable to resume activity {net.wswld.dtype.writer/net.wswld.dtype.writer.OptionsActivity}: java.lang.NullPointerException
12-23 15:37:59.960: E/AndroidRuntime(279): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
12-23 15:37:59.960: E/AndroidRuntime(279): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
12-23 15:37:59.960: E/AndroidRuntime(279): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
12-23 15:37:59.960: E/AndroidRuntime(279): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-23 15:37:59.960: E/AndroidRuntime(279): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-23 15:37:59.960: E/AndroidRuntime(279): at android.os.Handler.dispatchMessage(Handler.java:99)
12-23 15:37:59.960: E/AndroidRuntime(279): at android.os.Looper.loop(Looper.java:123)
12-23 15:37:59.960: E/AndroidRuntime(279): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-23 15:37:59.960: E/AndroidRuntime(279): at java.lang.reflect.Method.invokeNative(Native Method)
12-23 15:37:59.960: E/AndroidRuntime(279): at java.lang.reflect.Method.invoke(Method.java:521)
12-23 15:37:59.960: E/AndroidRuntime(279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-23 15:37:59.960: E/AndroidRuntime(279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-23 15:37:59.960: E/AndroidRuntime(279): at dalvik.system.NativeStart.main(Native Method)
12-23 15:37:59.960: E/AndroidRuntime(279): Caused by: java.lang.NullPointerException
12-23 15:37:59.960: E/AndroidRuntime(279): at net.wswld.dtype.writer.DTypeActivity.onResume(DTypeActivity.java:109)
12-23 15:37:59.960: E/AndroidRuntime(279): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
12-23 15:37:59.960: E/AndroidRuntime(279): at android.app.Activity.performResume(Activity.java:3823)
12-23 15:37:59.960: E/AndroidRuntime(279): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)