Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从非活动类获取非活动的SharedReference_Java_Android_Android Context - Fatal编程技术网

Java 如何从非活动类获取非活动的SharedReference

Java 如何从非活动类获取非活动的SharedReference,java,android,android-context,Java,Android,Android Context,在我的活动类中,我将一些数据存储在SharedReference中。我的非活动类有getter和setter来读取SharedReference数据。我想在另一个活动类中使用此数据。但我总是得到一个空指针异常 这是我的活动类中的代码片段 SharedPreferences prefs; prefs = getActivity().getSharedPreferences(KEY_NAME_FOR_PREF, 0); SharedPreferences.Editor editor

在我的活动类中,我将一些数据存储在
SharedReference
中。我的非活动类有getter和setter来读取
SharedReference
数据。我想在另一个活动类中使用此数据。但我总是得到一个空指针异常

这是我的活动类中的代码片段

SharedPreferences prefs;

prefs = getActivity().getSharedPreferences(KEY_NAME_FOR_PREF, 0);
        SharedPreferences.Editor editor = prefs.edit();
editor.putString("name", name.getText().toString());
editor.commit();
这是我的第一节非活动课

private String name;
private Context mContext;

public PdfConsultantContacts(Context context){
    mContext = context;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
这是我的第二节非活动课。 语境; PdfConsultantContacts联系人=新的PdfConsultantContacts(mContext)

当我尝试
contact.getName()时我总是得到一个空指针异常,但我知道
SharedReference
保存正确

  • 确保活动中的“代码片段”已真正执行,您可以通过打印一些日志来验证它

  • 确保\u PREF的键\u NAME \u等于“app\u prefs”


  • 创建一个包含两个静态方法的类,并在整个应用程序中使用它来存储和检索来自SharedReference的数据

    public class Utils {    
                public static void putStringInPreferences(Context context, String value, String key, String pKey) {
                    SharedPreferences sharedPreferences = context.getSharedPreferences(pKey, Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString(key, value);
                    editor.commit();
                }
    
                public static String getStringFromPreferences(Context context,String defaultValue, String key, String pKey) {
                    SharedPreferences sharedPreferences = context.getSharedPreferences(pKey, Context.MODE_PRIVATE);
                    String temp = sharedPreferences.getString(key, defaultValue);
                    return temp;
                }
        }
    

    默认值为空。。。您如何知道首选项已保存?请尝试mContext.getSharedReferences(“app_prefs”,0);您能说出您在哪里调用initContact()吗?在活动类还是非活动类中?我知道这一点,因为我使用textview来显示SharedReference值。我在非活动类中使用它。当我像这样使用它时,我会得到相同的错误:testKlasse.getStringFromPreferences(mContext,null,“name”,“app_prefs”);
    public class Utils {    
                public static void putStringInPreferences(Context context, String value, String key, String pKey) {
                    SharedPreferences sharedPreferences = context.getSharedPreferences(pKey, Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString(key, value);
                    editor.commit();
                }
    
                public static String getStringFromPreferences(Context context,String defaultValue, String key, String pKey) {
                    SharedPreferences sharedPreferences = context.getSharedPreferences(pKey, Context.MODE_PRIVATE);
                    String temp = sharedPreferences.getString(key, defaultValue);
                    return temp;
                }
        }