【发布时间】:2014-03-19 12:22:22
【问题描述】:
这是我的用户设置主题的代码:
case R.id.darkorangetheme:
ThemeChanger.onActivityCreateSetTheme(this, ThemeChanger.THEME_DARKORANGE);
editor.putInt("mytheme", appliedtheme);
editor.commit();
return true;
case R.id.bluetheme:
ThemeChanger.onActivityCreateSetTheme(this, ThemeChanger.THEME_BLUE);
editor.putInt("mytheme", appliedtheme);
editor.commit();
return true;
case R.id.greentheme:
ThemeChanger.onActivityCreateSetTheme(this, ThemeChanger.THEME_GREEN);
editor.putInt("mytheme", appliedtheme);
editor.commit();
return true;
default: return super.onOptionsItemSelected(item);
这是我的主题转换器类的代码:
package com.example.calculator;
导入android.support.v7.app.ActionBarActivity;
公共类 ThemeChanger { 私有静态 int sTheme;
public final static int THEME_DARKORANGE = 0;
public final static int THEME_GREEN = 1;
public final static int THEME_BLUE = 2;
public final static int THEME_LIGHT = 3;
public static void onActivityCreateSetTheme(ActionBarActivity activity, int theme)
{
switch (sTheme)
{
default:
case THEME_DARKORANGE:
activity.setTheme(R.style.Theme_Darkorange);
break;
case THEME_GREEN:
activity.setTheme(R.style.Theme_Green);
break;
case THEME_BLUE:
activity.setTheme(R.style.Theme_Blue);
break;
case THEME_LIGHT:
activity.setTheme(R.style.Theme_AppCompat_Light);
}
}
}
现在我的 onCreate 方法:
public class MainActivity extends ActionBarActivity
{
private TextView inputText,resultText,memoryStatText;
public static int button1,buttoncos,buttonmadd;
double firstNumber=0,secondNumber=0,result=0;
int firstOperand=0,TotalOperator=0;
Stack<String> mInputStack;
Stack<String> mOperationStack;
boolean resetInput = false;
boolean hasFinalResult = false;
int appliedtheme;
String mDecimalSeparator;
double memoryValue = Double.NaN;
SharedPreferences preferences = null;
SharedPreferences.Editor editor = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
int defaultValue = R.drawable.blue;
int themedefault = ThemeChanger.THEME_BLUE;
appliedtheme = preferences.getInt("mytheme", themedefault);
ThemeChanger.onActivityCreateSetTheme(this,appliedtheme);
setContentView(R.layout.main);
button1 = preferences.getInt("DigitButtonStyle",defaultValue);
buttonmadd = preferences.getInt("MemoryButtonStyle",defaultValue);
buttoncos = preferences.getInt("FunctionButtonStyle",defaultValue);
现在我的问题是为什么我的应用程序崩溃了?
这是我的日志
03-19 08:02:05.298: E/AndroidRuntime(3217): FATAL EXCEPTION: main
03-19 08:02:05.298: E/AndroidRuntime(3217): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.NullPointerException
03-19 08:02:05.298: E/AndroidRuntime(3217): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
03-19 08:02:05.298: E/AndroidRuntime(3217): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
03-19 08:02:05.298: E/AndroidRuntime(3217): at android.app.ActivityThread.access$600(ActivityThread.java:162)
03-19 08:02:05.298: E/AndroidRuntime(3217): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
03-19 08:02:05.298: E/AndroidRuntime(3217): at android.os.Handler.dispatchMessage(Handler.java:107)
03-19 08:02:05.298: E/AndroidRuntime(3217): at android.os.Looper.loop(Looper.java:194)
03-19 08:02:05.298: E/AndroidRuntime(3217): at android.app.ActivityThread.main(ActivityThread.java:5371)
03-19 08:02:05.298: E/AndroidRuntime(3217): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 08:02:05.298: E/AndroidRuntime(3217): at java.lang.reflect.Method.invoke(Method.java:525)
03-19 08:02:05.298: E/AndroidRuntime(3217): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-19 08:02:05.298: E/AndroidRuntime(3217): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-19 08:02:05.298: E/AndroidRuntime(3217): at dalvik.system.NativeStart.main(Native Method)
03-19 08:02:05.298: E/AndroidRuntime(3217): Caused by: java.lang.NullPointerException
03-19 08:02:05.298: E/AndroidRuntime(3217): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:161)
03-19 08:02:05.298: E/AndroidRuntime(3217): at com.example.calculator.MainActivity.<init>(MainActivity.java:38)
03-19 08:02:05.298: E/AndroidRuntime(3217): at java.lang.Class.newInstanceImpl(Native Method)
03-19 08:02:05.298: E/AndroidRuntime(3217): at java.lang.Class.newInstance(Class.java:1319)
03-19 08:02:05.298: E/AndroidRuntime(3217): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-19 08:02:05.298: E/AndroidRuntime(3217): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
03-19 08:02:05.298: E/AndroidRuntime(3217): ... 11 more
【问题讨论】:
-
相关:您总是将
0保存到mytheme首选项,而不是用户选择的主题。 -
是的,这是一个错误,谢谢。但是,为什么我的应用程序在设置为零时会崩溃,它应该使我的主题变成橙色而不是崩溃。
-
啊,我现在在您的其他文件中看到了定义。无论如何,提供更多代码。
preferences在哪里实例化?最早可以在onCreate()中完成。我也看不到你从哪里打电话给onActivityCreateSetTheme。我还认为,一旦它不崩溃,你应该得到一个无限重启循环。 -
是的,你是对的,我编辑了我的代码,它的工作但进入了无限循环。为什么会这样。
-
@A--C 是在正确的轨道上....不过,如果您只是显示实际失败的代码,那么提供帮助会容易得多。错误在 MainActivity 的构造函数中...
标签: android