简体   繁体   English

android Sharedpreferences 非活动 class

[英]android Sharedpreferences non-activity class

private static String geturl() {
        Context applicationContext = configActivity.getContextOfApplication();
//        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);

        SharedPreferences prefs =applicationContext.getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
        String url = prefs.getString("url", "No name defined");
        return url;
    }

This method is in a non-activity class i tried to have my string from a non-activity class i tried to do like someone say there: Android - How to use SharedPreferences in non-Activity class? This method is in a non-activity class i tried to have my string from a non-activity class i tried to do like someone say there: Android - How to use SharedPreferences in non-Activity class?

but i have this error:但我有这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference

The problem is in the first line of code, the method getContextOfApplication() is returning a null value:问题出在第一行代码中,getContextOfApplication() 方法返回 null 值:

Context applicationContext =configActivity.getContextOfApplication();上下文 applicationContext =configActivity.getContextOfApplication();

Since the Context may have come to the end of its lifecycle, it is generally recommended to not store Context or Activity objects as static variables as it may cause memory leaks.由于 Context 可能已经到了其生命周期的尽头,因此通常建议不要将 Context 或 Activity 对象存储为 static 变量,因为这可能会导致 memory 泄漏。 Instead, you should wrap a Context or Activity in a WeakReference and when needed, check to see if the context is not null after unwrappping.相反,您应该将 Context 或 Activity 包装在 WeakReference 中,并在需要时检查展开后上下文是否不是 null。

In the example below, an activity is stored as a WeakReference and unwrapped with get() in the postExecute() method when required.在下面的示例中,活动被存储为 WeakReference,并在需要时使用 postExecute() 方法中的 get() 解包。 Since the activity may have ended before the AsyncTask ends, this ensures no memory leaks and no NullPointer exceptions.由于活动可能在 AsyncTask 结束之前结束,这确保没有 memory 泄漏和 NullPointer 异常。

private class MyAsyncTask extends AsyncTask<String, Void, Void> {

    private WeakReference<Activity> mActivity;

    public MyAsyncTask(Activity activity) {
        mActivity = new WeakReference<Activity>(activity);
    }

    @Override
    protected void onPreExecute() {
        final Activity activity = mActivity.get();
        if (activity != null) {
            ....
        }
    }

    @Override
    protected Void doInBackground(String... params) {
        //Do something
        String param1 = params[0];
        String param2 = params[1];
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        final Activity activity = mActivity.get();
        if (activity != null) {
            activity.updateUI();
        }
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM