开发者

setOnPreferenceClickListener Help, Forclosing when activity launches

I know this cannot be as hard I think it is. I am sure I have everything that I can suppose to to implement my onClickListener. Am I missing something, why does this forceclose? I have provided the stack to take a look at. Any help would be great. This was suppose to be a simple activity to save user settings. If I comment out this code the activity displays the activity.

Preference customPref = (Preference) findPreference("customPref");

customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { //this line forcecloses every time i run the activity.
    @Override
    public boolean onPreferenceClick(Preference preference) {
        Toast.makeText(getBaseContext(),
                       "The custom preference has been clicked",
                       Toast.LENGTH_LONG)
             .show();
        SharedPreferences customSharedPreference = getSharedPreferences(
                "myCustomSharedPrefs", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = customSharedPreference.edit();
        editor.putString("myCustomPref", "The preference has been clicked");
        editor.commit(开发者_JAVA技巧);
        return true;
    }
});

Logs:

07-10 16:36:47.137: WARN/System.err(1042): java.lang.NullPointerException
07-10 16:36:47.147: WARN/System.err(1042):     at com.nyneaxis.android.mpg.Preferences.onCreate(Preferences.java:22)
07-10 16:36:47.147: WARN/System.err(1042):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-10 16:36:47.157: WARN/System.err(1042):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
07-10 16:36:47.157: WARN/System.err(1042):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
07-10 16:36:47.157: WARN/System.err(1042):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
07-10 16:36:47.157: WARN/System.err(1042):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
07-10 16:36:47.167: WARN/System.err(1042):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-10 16:36:47.167: WARN/System.err(1042):     at android.os.Looper.loop(Looper.java:123)
07-10 16:36:47.167: WARN/System.err(1042):     at android.app.ActivityThread.main(ActivityThread.java:4363)
07-10 16:36:47.167: WARN/System.err(1042):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 16:36:47.177: WARN/System.err(1042):     at java.lang.reflect.Method.invoke(Method.java:521)
07-10 16:36:47.177: WARN/System.err(1042):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-10 16:36:47.177: WARN/System.err(1042):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-10 16:36:47.177: WARN/System.err(1042):     at dalvik.system.NativeStart.main(Native Method)


It looks like your customPref variable is null. Make sure you have preference with key customPref in your preference xml (if you use one) or maybe you've misspelled the key there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜