我正在使用服务器底层应用程序,因此每当用户登录时,我必须捕获其唯一的userId并在我的整个项目中的不同活动中使用它。 因此,在从JSONObject获取userId之后,我将它放入sharedPrederence并将其置于另一个活动中但是在进入我的主要活动之后,在成功登录之后,共享首选项向我显示userId的默认值为零。
这是我在PostData.java类中的代码,它将在登录活动的后台运行:
private SharedPreferences userIdSharedPreferences;
private SharedPreferences.Editor userIdEditor;
try {
JSONObject obj = new JSONObject(response.toString());
//boolean requestStatus = obj.getBoolean("requestStatus");
JSONObject data = obj.getJSONObject("data");
userId = Integer.parseInt(data.getString("userId"));
userIdEditor = userIdSharedPreferences.edit();
userIdEditor.putInt("crrUserId", userId);
userIdEditor.apply();
userIdEditor.commit();
} catch (JSONException e) {
e.printStackTrace();
}
以下是我在其他活动中使用该sharedPreference值的方法:
userIdSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
crrUserId = userIdSharedPreferences.getInt("crrUserId", 0);
Toast.makeText(getApplicationContext(), "Your user id is: " + crrUserId, Toast.LENGTH_SHORT).show(); //This toast showing 0 which is default i set.
修改
为了更清楚,我现在使用以下函数将我的userId从我的后台类PostData.java传递给我的LoginActivity:
Integer passUserId() {
return userId;
}
从那里我使用sharedPreference传递userId,如下所示:
Intent loginSuccess = new Intent(getApplicationContext(), MainActivity.class);
Integer crrUserId = Login.passUserId(); //Login is name of variable which will accespassUserId function in PostData.java class
SharedPreferences userIdSharedPreferences = getApplicationContext().getSharedPreferences("userId", 0);
SharedPreferences.Editor userIdEditor;
userIdEditor = userIdSharedPreferences.edit();
userIdEditor.putInt("crrUserId", crrUserId);
userIdEditor.apply();
startActivity(loginSuccess);
finish();
以下是我在主要活动中使用的方式:
public class MainActivity extends AppCompatActivity {
SharedPreferences userIdSharedPreferences;
SharedPreferences.Editor userIdEditor;
Integer crrUserId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userIdSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
crrUserId = userIdSharedPreferences.getInt("userId", 0);
Toast.makeText(getApplicationContext(), "Your user id is: " + crrUserId, Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:1)
初始化userIdSharedPreferences可能就是问题
try {
JSONObject obj = new JSONObject(response.toString());
//boolean requestStatus = obj.getBoolean("requestStatus");
JSONObject data = obj.getJSONObject("data");
userId = Integer.parseInt(data.getString("userId"));
SharedPreferences userIdSharedPreferences = getApplicationContext().getSharedPreferences("MyPref", 0);
SharedPreferences.Editor userIdEditor = userIdSharedPreferences.edit();
userIdEditor.putInt("crrUserId", userId);
userIdEditor.apply(); //apply or commit not both
userIdEditor.commit();
} catch (JSONException e) {
e.printStackTrace();
}
答案 1 :(得分:1)
试试这个:
首先,您需要初始化userIdSharedPreferences
对象,如下所示:
userIdSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
获得SharedPreference
的对象后执行此操作:
userIdEditor = userIdSharedPreferences.edit();
获得SharedPreferences.Editor
的对象完成任务后。