-1

In Android I suddenly get an error

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.preference.Preference.setOnPreferenceClickListener(android.preference.Preference$OnPreferenceClickListener)' on a null object reference
    at com.impyiablue.stoxx.UserSettingActivity.onCreate(UserSettingActivity.java:32)
    at android.app.Activity.performCreate(Activity.java:5977)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) 
    at android.app.ActivityThread.access$800(ActivityThread.java:148) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5272) 
    at java.lang.reflect.Method.invoke(Native Method) 
    ... 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 

in this piece of code:

public class UserSettingActivity extends PreferenceActivity {

    private Preference myPreference;
    MainActivity.MyCallBack callBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // replaced on 5.1.2016
        // http://stackoverflow.com/questions/6822319/what-to-use-instead-of-addpreferencesfromresource-in-a-preferenceactivity
        //addPreferencesFromResource(R.xml.preferences);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();

        myPreference = findPreference("reset");
        myPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
             public boolean onPreferenceClick(Preference arg0) {
             ...

and the preferences.xml file looks as follows:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <EditTextPreference android:title="Your Name"
        android:key="username"
        android:summary="Please provide your username"></EditTextPreference>
    <CheckBoxPreference android:title="Application Updates"
        android:defaultValue="false"
        android:summary="This option if selected will allow the application to check for latest versions."
        android:key="applicationUpdates" />

    <Preference
        android:key="reset"
        android:title="Reset database"
        android:summary="This will remove every entry in the database"
         />
</PreferenceScreen>

It worked before (I cannot specify before what time). I just want to have an item 'reset' in my settings which will delete the database of the app. The method findPreference is striked through (implying it is some outdated code, without any clue how to 'fix' it).

How can I fix this null pointer exception?

appersiano
  • 2,670
  • 22
  • 42
Alex
  • 41,580
  • 88
  • 260
  • 469
  • adding a fragment is not synchronous. you should move the logic related to the preference in the fragment itself – njzk2 Jan 15 '16 at 20:38
  • no idea what you are taking about. What the heck is a Fragment? I just want to start some code when the user selects 'reset' in the user settings... – Alex Jan 15 '16 at 20:40
  • 1
    `getFragmentManager().beginTransaction()`, `new MyPreferenceFragment()`, `What the heck is a Fragment?` are you for real? – njzk2 Jan 15 '16 at 20:41
  • go back to the question you quote, it links to the doc for `PreferenceActivity`, with examples of how to use it with `PreferenceFragment`, which `MyPreferenceFragment` should be, btw. – njzk2 Jan 15 '16 at 20:45
  • I am. I have no idea what a Fragment is. I guess it is some weird concept to make programming in Android incredible cumbersome on purpose. Am I right? – Alex Jan 15 '16 at 20:45
  • 1
    you could say that. but that would not help you. Fragments have been around for almost 5 years, and generalized throughout the platform 4 years ago. It has pros and cons. They are here to help you organize your components. In your case, it mostly mean putting your logic in your fragment, and have the Activity simply load the fragment. You can also just use deprecated function. – njzk2 Jan 15 '16 at 20:48
  • Why do I need to have the 'Activity' load the 'Fragment'? I do not understand this sentence at all? Can you explain in english? – Alex Jan 15 '16 at 20:51
  • you are already doing that. the transaction with the fragment manager. you now have to find your own way, I can only wave in the general direction: http://developer.android.com/guide/components/fragments.html – njzk2 Jan 15 '16 at 21:02

2 Answers2

3

After

super.onCreate(savedInstanceState);

add line

addPreferencesFromResource(R.xml.preferences);

addPreferencesFromResource needs to be called before you do anything with the preference.

apmartin1991
  • 3,064
  • 1
  • 23
  • 44
  • This works, but it gets me totally confused! reason: The text 'addPreferencesFromResource' is striked through!! Android Studio tells me that this code is outdated! So I followed the instructions given here: http://stackoverflow.com/questions/6822319/what-to-use-instead-of-addpreferencesfromresource-in-a-preferenceactivity to FIX the code. But instead the code is changed to crash!! So what should I do when I see some code pieces which are stiked through? Ignore it? Why does AndroidStudoi does this? To change the code so it does not work anymore? HELP!!! – Alex Jan 15 '16 at 20:43
  • because all of android migrated to using fragment in ui, about 4 (!) years ago. – njzk2 Jan 15 '16 at 20:44
  • So I am using very old concepts?`Is that what you are trying to say? – Alex Jan 15 '16 at 20:46
  • Striked through code means it is depreciated. While depreciated code should be avoided, most of the time it won't cause any harm. Sadly while addPreferencesFromResource has been replaced, it is only usable from API 11. Which means if you were using API 10 or below you would have more work to do. – apmartin1991 Jan 15 '16 at 20:46
  • Alex, if this fixed this issue then please do mark the answer as correct :) – apmartin1991 Jan 15 '16 at 20:47
  • While I agree Fragments are the way forward, Fragments with complicated UIs cause so many issues and can be hard to get the hang of. However when you know how to use them nicely they are amazing! – apmartin1991 Jan 15 '16 at 20:48
  • Yes I will mark it as answer. But I still do not understand why this all is so incredible comlicated? Why? What does it all mean? – Alex Jan 15 '16 at 20:48
  • But why should I use a fragment? I print the word 'reset' on the screen, and when the user taps on it, some actions shall start. How does a Fragment fits into this simple user action? – Alex Jan 15 '16 at 20:49
  • The code your using has been replaced over 10 API's ago. Ideally you should move forward to the newer and better ways of doing it. Now without looking at your project, understanding the flow then I can't really advise you on what would be best. A fragment is just a view within an activity. – apmartin1991 Jan 15 '16 at 20:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100808/discussion-between-alex-and-apmartin1991). – Alex Jan 15 '16 at 20:51
  • Nobody really uses PreferenceFragment. I wouldn't have any problems with using that API. Fragments frequently have a use, but their use in preference screens is pretty minimal. Just go ahead and use the older API. – Gabe Sechan Jan 15 '16 at 21:04
0

You haven't got the addPreferencesFromResource in your onCreate() function, so the preference xml isn't loaded at this time.

Use PreferenceActivity#addPreferencesFromResource(R.xml.preferences);

Source: http://rominirani.com/android-preferences-tutorial/

CreepPlays
  • 56
  • 3
  • if you read the question, you'll see that the OP is aware of that, and what more, aware also of the fact that this is deprecated. – njzk2 Jan 15 '16 at 20:43
  • deprecated::: http://stackoverflow.com/questions/6822319/what-to-use-instead-of-deprecatedaddpreferencesfromresource-in-a-preferenceactivity – Alex Jan 15 '16 at 20:43