Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.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 在Android中检索SharedReference_Java_Android_Android Activity_Sharedpreferences - Fatal编程技术网

Java 在Android中检索SharedReference

Java 在Android中检索SharedReference,java,android,android-activity,sharedpreferences,Java,Android,Android Activity,Sharedpreferences,我正在构建我的第一个多活动应用程序。我在Activity1中有一些由用户定义的地理坐标,这些坐标保存到SharedReferences: // these strings are used when saving the users' preferred location private static final String POINT_LATITUDE_KEY = "POINT_LATITUDE_KEY"; private static final String POINT_LONGITUD

我正在构建我的第一个多活动应用程序。我在Activity1中有一些由用户定义的地理坐标,这些坐标保存到
SharedReferences

// these strings are used when saving the users' preferred location
private static final String POINT_LATITUDE_KEY = "POINT_LATITUDE_KEY";
private static final String POINT_LONGITUDE_KEY = "POINT_LONGITUDE_KEY";

private static final String TAG = "Activity1";
// actually saves the coordinates to the preferences 
private void saveCoordinatesInPreferences(float latitude, float longitude) {
    SharedPreferences prefs = 
       this.getSharedPreferences(getClass().getSimpleName(),
                       Context.MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = prefs.edit();
    prefsEditor.putFloat(POINT_LATITUDE_KEY, latitude);
    prefsEditor.putFloat(POINT_LONGITUDE_KEY, longitude);
    //Log.i(TAG, "latitude is: " + latitude);
    //Log.i(TAG, "longitude is: " + longitude);
    prefsEditor.commit();
}
这些共享参考坐标随后需要由Activity2使用。我找不到它们。这是我为检索而编写的一个方法。我的变量
纬度
未写入
日志

private static final String TAG = "Activity2";

protected void getLatLongPref() {
// adapted from http://mrbool.com/android-shared-preferences-managing-files-using-internal-external-storage/30508
// accessed April 10, 2015
    SharedPreferences pref = getApplicationContext().getSharedPreferences("POINT_LATITUDE_KEY", MODE_PRIVATE);
    float latitudeUser = pref.getFloat("POINT_LATITUDE_KEY", 0); // getting users chosen latitude   
    Log.i(TAG, "latitude is: " + latitudeUser);

}

您认为我做错了什么?

更改您的第一个代码段,如下所示:

private void saveCoordinatesInPreferences(float latitude, float longitude) {
    SharedPreferences prefs = 
       this.getSharedPreferences("myPref", Context.MODE_PRIVATE);
    [...]
}
protected void getLatLongPref() {
    SharedPreferences pref = 
       this.getSharedPreferences("myPref", Context.MODE_PRIVATE);
    [...]
}
…而你的第二个是这样的:

private void saveCoordinatesInPreferences(float latitude, float longitude) {
    SharedPreferences prefs = 
       this.getSharedPreferences("myPref", Context.MODE_PRIVATE);
    [...]
}
protected void getLatLongPref() {
    SharedPreferences pref = 
       this.getSharedPreferences("myPref", Context.MODE_PRIVATE);
    [...]
}

基本上,这两种情况都必须相同。另请参见。

您对这两个SharedReference使用了错误的上下文和首选项名称。将第一个更改为:

SharedPreferences prefs = getApplicationContext().getSharedPreferences("POINT_LATITUDE_KEY",
                       Context.MODE_PRIVATE);

在两个活动中尝试使用
this.getDefaultSharedReferences()
。还有,您的第一个代码段中的
POINT\u LATITUDE\u键是什么?另外,请参阅如何查看实际放入SharedPref的内容(无需解析xml)。@JonasCz ah抱歉,
点纬度键
点经度键
是我定义要添加到prefs的键。我已经编辑了我的question@JonasCz
this.getDefaultSharedReferences()
不可用,链接中提到的DDMS文件夹为空。此功能正常。你刚刚打败了@JonasCz!谢谢两位。总之,我创建了2个
pref
对象:
SharedReferences prefLat=this.getSharedReferences(“点纬度键”,Context.MODE\u私有);SharedReferences prefLong=this.getSharedReferences(“点经度键”,Context.MODE\u私有)
@mark,在这两种情况下,在放置数据和获取数据时应使用相同的引用名称。