.getContext()/ this在PreferenceActivity

时间:2015-06-15 17:24:41

标签: android null sharedpreferences

我正在创建一个应用程序,其中有一个登录AlertDialog,这可以让你选择你的位置 但是,当您更改设置中的位置时,您当然需要使用不同的凭据重新登录 为此,我使用了onPreferenceClick 但是,每当我尝试打开设置时,我都会得到这个:

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)

这是我的代码:

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;

            }
        });

    }
}

当我运行调试器时,它告诉我savedInstanceState = null
第114行是SharedPreferences MainPrefs = PreferenceManager.getDefaultSharedPreferences(this);
如果没有调用这个我当然无法更新我的偏好,我更新的首选项在登录对话框完成时设置为true,并在单击“更改位置”首选项时设置为false。

我不知道为什么这是错误的,它似乎适用于Preference versiePreference website

有办法做到这一点吗?

编辑:

enter image description here

现在行:filiaal.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {似乎是问题,有没有办法检查为什么这是null?

0 个答案:

没有答案