把pref崩溃的android

时间:2013-07-08 08:02:00

标签: android preferences

我一直试图使用prefs一周试图实现颜色方案pref。

虽然一切正常,但仍有一些奇怪的事情发生。 它当我执行“selPreset”方法时,pref不会立即更新,当我关闭设置屏幕然后再次打开时,它们会更新。 虽然有时应用程序崩溃。

如果有人可以帮我解决,那就太棒了。

Code:
StripedEngine() {
            mPrefs = StripedMain.this
                    .getSharedPreferences(SHARED_PREFS_NAME, 0);
            mPrefs.registerOnSharedPreferenceChangeListener(this);
            drawStriped();
            onSharedPreferenceChanged(mPrefs, null);
        }


public void onSharedPreferenceChanged(SharedPreferences prefs,
                    String key) {
            stripeType = prefs.getString("stripe_type", "hor");

            stripeSpacetemp = prefs.getString("space1", null);
            if (stripeSpacetemp != null) {
                stripeSpace = Integer.valueOf(stripeSpacetemp);
            }

            verHex = prefs.getInt("verColKey", 0xffb41212);
            horHex = prefs.getInt("horColKey", 0xfffc6161);

            verStartHex = prefs.getInt("verStartKey", 0);
            horStartHex = prefs.getInt("horStartKey", 0);
            verEndHex = prefs.getInt("verEndKey", 0);
            horEndHex = prefs.getInt("horEndKey", 0);

            colorType = prefs.getString("color_type", "solid");
            bgCol = prefs.getInt("bgKey", Color.WHITE);

            transX = Integer.valueOf(prefs.getString("transX", "0"));
            transY = Integer.valueOf(prefs.getString("transY", "0"));

            if (prefs.getString("pre_type", "1").equals(preVal)) {
            } else 
            {
                preVal = prefs.getString(key, "1");
                selPreset(Integer.valueOf(preVal));

            }

            drawStriped();
        }

这是方法:

selPreset:
private void selPreset(int prefix) {
            android.util.Log.d("StripedLog","run 2");
            switch (prefix) {
            case 1:
                colorType = "solid";
                verHex = 0xffaa16f7;
                horHex = 0xffffea00;
                stripeSpace = 10;
                bgCol = 0xff000000;
                stripeType = "ver";

                mPrefs.edit().putString("color_type", "solid").commit();
                mPrefs.edit().putInt("verColKey", 0xffaa16f7).commit();
                mPrefs.edit().putInt("horColKey", 0xffffea00).commit();



                mPrefs.edit().putString("space1", String.valueOf(10)).commit();
                mPrefs.edit().putInt("bgKey",0xff000000)
                        .commit();
                mPrefs.edit().putString("stripe_type", "ver").commit();

                break;
            case 2:

                colorType = "grad";
                verStartHex = 0xff710866;
                verEndHex = 0xff9e1b90;
                horStartHex = 0xff094171;
                horEndHex = 0xff1764a6;
                stripeSpace = 10;
                stripeType = "both";
                bgCol = 0xffffffff;

                mPrefs.edit().putString("color_type", "grad").commit();

                mPrefs.edit()
                        .putInt("verStartKey", (0xff710866))
                        .commit();
                mPrefs.edit()
                        .putInt("horStartKey",(0xff094171))
                        .commit();
                mPrefs.edit().putInt("verEndKey", (0xff9e1b90))
                        .commit();
                mPrefs.edit().putInt("horEndKey", (0xff1764a6))
                        .commit();


                mPrefs.edit().putString("space1", String.valueOf(10)).commit();


                    mPrefs.edit().putInt("bgKey", 0xffffffff)
                        .commit();
                mPrefs.edit().putString("stripe_type", "both").commit();

                break;
            }

如果您需要信息,请发表评论。

日志:

07-08 13:38:09.359: E/AndroidRuntime(30694): FATAL EXCEPTION: main
07-08 13:38:09.359: E/AndroidRuntime(30694): java.lang.NumberFormatException: Invalid int: "ver"
07-08 13:38:09.359: E/AndroidRuntime(30694):    at java.lang.Integer.invalidInt(Integer.java:138)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at java.lang.Integer.parse(Integer.java:375)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at java.lang.Integer.parseInt(Integer.java:366)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at java.lang.Integer.parseInt(Integer.java:332)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at java.lang.Integer.valueOf(Integer.java:490)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at com.pack.Striped.StripedMain$StripedEngine.onSharedPreferenceChanged(StripedMain.java:160)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at android.app.SharedPreferencesImpl$EditorImpl.notifyListeners(SharedPreferencesImpl.java:455)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at android.app.SharedPreferencesImpl$EditorImpl.commit(SharedPreferencesImpl.java:441)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at com.pack.Striped.StripedMain$StripedEngine.selPreset(StripedMain.java:386)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at com.pack.Striped.StripedMain$StripedEngine.onSharedPreferenceChanged(StripedMain.java:160)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at com.pack.Striped.StripedMain$StripedEngine.<init>(StripedMain.java:111)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at com.pack.Striped.StripedMain.onCreateEngine(StripedMain.java:57)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1072)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:61)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at android.os.Looper.loop(Looper.java:137)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at android.app.ActivityThread.main(ActivityThread.java:4935)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at java.lang.reflect.Method.invokeNative(Native Method)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at java.lang.reflect.Method.invoke(Method.java:511)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
07-08 13:38:09.359: E/AndroidRuntime(30694):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我认为您的问题来自此代码:

if (stripeSpacetemp != null) {
   stripeSpace = Integer.valueOf(stripeSpacetemp);
}

听起来var stripeSpacetemp不是null,但也不是Integer,但包含值“ver”。你应该至少尝试/捕捉方法调用。