.getContext() / this produces null object reference inside PreferenceActivity

56 Views Asked by At

I am making an app in which there is a login AlertDialog, this lets you choose you location.
But, when you change the location in the settings you of course need to relog using different credentials.
I used an onPreferenceClick for this.
But, whenever i try to open the settings i get this:

Process: nl.knapper_development.www.jrw, PID: 9068
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{nl.knapper_development.www.jrw/nl.knapper_development.www.jrw.instelling}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
            at android.app.ActivityThread.access$800(ActivityThread.java:149)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:211)
            at android.app.ActivityThread.main(ActivityThread.java:5333)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
            at android.content.ContextWrapper.getPackageName(ContextWrapper.java:131)
            at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:374)
            at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:369)
            at nl.knapper_development.www.jrw.instelling.<init>(instelling.java:114)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.Class.newInstance(Class.java:1572)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
            at android.app.ActivityThread.access$800(ActivityThread.java:149)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:211)
            at android.app.ActivityThread.main(ActivityThread.java:5333)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

This is my code:

public class instelling extends PreferenceActivity{

    final String loginScreenShownPref = "loginScreenShown";
    private static final boolean ALWAYS_SIMPLE_PREFS = true;


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

        setupSimplePreferencesScreen();
    }

    private void setupSimplePreferencesScreen() {
        if (!isSimplePreferences(this)) {
            return;
        }

        //Voegd prefrences toe
        addPreferencesFromResource(R.xml.pref_locatie);
        addPreferencesFromResource(R.xml.pref_thema);
        addPreferencesFromResource(R.xml.pref_informatie);

        // Parsed de website
        Preference website = findPreference("website_knop");
        website.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

            public boolean onPreferenceClick(Preference preference) {
                startActivity(new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://knapper-development.nl")));
                return true;
            }
        });

        // Versie Toast
        Preference versie = findPreference("versie_knop");
        versie.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {

                String version = "";
                try {
                    version = getApplicationContext().getPackageManager().getPackageInfo(getApplicationContext().getPackageName(), 0).versionName;
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }

                Toast.makeText(instelling.this, version, Toast.LENGTH_LONG).show();
                return true;
            }
        });


    }

    @Override
    public boolean onIsMultiPane() {
        return isXLargeTablet(this) && !isSimplePreferences(this);
    }

    private static boolean isXLargeTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
    }

    private static boolean isSimplePreferences(Context context) {
        return ALWAYS_SIMPLE_PREFS
                || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
                || !isXLargeTablet(context);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class InformatiePreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_informatie);
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class ThemaPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_thema);
        }
    }

    SharedPreferences MainPrefs = PreferenceManager.getDefaultSharedPreferences(this);

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

        Preference filiaal = this.findPreference("filiaal_lijst");
        filiaal.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference preference) {

                SharedPreferences.Editor prefEditor = MainPrefs.edit();
                prefEditor.putBoolean(loginScreenShownPref, false);
                prefEditor.apply();
                return true;

            }
        });

    }
}

When i run the debugger it tells me that savedInstanceState = null
Line 114 is SharedPreferences MainPrefs = PreferenceManager.getDefaultSharedPreferences(this);
Without calling this i of course cannot update my preference, the preference i am updating is set to true when the login dialog is completed, and set to false when the "Change Location" preference is clicked.

I don't know why this is wrong, it seems to work for Preference versie and Preference website

Is there a way to do this?

EDIT:

enter image description here

now the line: filiaal.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { seem to be the problem, is there a way to check why this is null?

0

There are 0 best solutions below