-1

Is there a way to perform an action related to a Preference in a PreferenceActivity? I've been using this:

CheckBoxPreference thing = (CheckBoxPreference) findPreference("thing");
thing.setChecked(true);

The first line runs okay but the second line results in a NullPointerException. These lines are inside the onCreate() method in my code. Ideally I want to use an onClicked() signal so that I can run certain methods when preferences are changed. Does anyone know what I'm doing wrong?

Joel Auterson
  • 728
  • 1
  • 7
  • 26
  • are there meant to be two different thing objects? or are you really trying to say thing is based on itself? – dten Sep 24 '11 at 10:46
  • Please provide a full context if you want useful answer. – Renaud Sep 24 '11 at 11:05
  • 1
    Apologies, it should have been "thing" in the second part - this refers to an object defined in XML. The code is correct, I just typed it in wrong here. – Joel Auterson Sep 24 '11 at 11:46

3 Answers3

0

I think you should be using an onSharedPreferenceChangedListener which you can set on a PreferenceScreen

http://developer.android.com/reference/android/preference/PreferenceScreen.html

dten
  • 2,364
  • 21
  • 28
0

thing is NULL -- > maybe then prefference with 'thing' refference doesn't exist

I recomend you to read this Question

Community
  • 1
  • 1
A.Quiroga
  • 5,704
  • 6
  • 37
  • 58
0

public Preference findPreference (CharSequence key), so what is the thing argument ⇒ try findPreference("thing")

Update: That blog entry may be of use to you.

Renaud
  • 8,783
  • 4
  • 32
  • 40