更改onResume中的变量void

时间:2012-12-23 12:51:56

标签: android android-activity

好的,所以下面一段代码放在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)

2 个答案:

答案 0 :(得分:1)

可能错误是您将最终变量发送到另一个方法。你为什么要发送EditText?当然你只需要发送edit_text.getText()的结果?你也应该使用这种安排而不是所有的if:

switch(Theme){
case 1:
//do something
break;
case 2:
//do something
break;
case 3:
//do something
break;
}

答案 1 :(得分:0)

好的,我似乎找到了解决方法。

我所做的是在MainActivity onResume中创建一个简单的布尔检查,检查选项是否确实已更改,然后才执行switch部分。

如果在OptionsActivity中按下了任何按钮,它会向数据库提交opt_changed布尔值,然后在onResume中检查它的值,然后再执行SetTheme...方法。< / p>

无论如何,感谢大家的回答和关注。我真的需要阅读Android空洞及其行为。