我正在嘗試在整個應(yīng)用程序中維護用戶 ID 和用戶名。我創(chuàng)建了一個名為 Session 的類,它將數(shù)據(jù)放置和接收到 SharedPreferences 并設(shè)置它工作得很好。我遇到的問題是當我去另一個活動中檢索它時。會話類構(gòu)造函數(shù) public Session(Context cntx) { // TODO Auto-generated constructor stub prefs = PreferenceManager.getDefaultSharedPreferences(cntx); }添加用戶ID的會話類方法和檢索它的方法 public void setUserID(int userID) { prefs.edit().putInt("userID", userID).apply(); }public int getID() { int userID; return userID = prefs.getInt("userID",0); }在登錄時設(shè)置 ID StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url, new Response.Listener<String>() { @Override public void onResponse(String response) { builder.setTitle("Server Response"); builder.setMessage("Response :"+response); session = new Session(getApplicationContext()); int responseInt = Integer.parseInt(response.trim()); session.setUserID(responseInt); Intent myIntent = new Intent(LoginActivity.this, navActivity.class); startActivity(myIntent); finish();嘗試檢索該 ID 并放入 editText @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_jars); session = new Session(getApplicationContext()); userID = (EditText) findViewById(R.id.userID); builder = new AlertDialog.Builder(MyJars.this); userID.setText(session.getID());我希望能夠提取該數(shù)據(jù)并將其發(fā)送到我的服務(wù)器,在那里我可以運行我的 PHP 腳本,就像我在其他活動中一樣,但這不合作。
1 回答

猛跑小豬
TA貢獻1858條經(jīng)驗 獲得超8個贊
在這一行:
userID.setText(session.getID());
session.getID()
是一個整數(shù),它被解釋為一個整數(shù)資源 ID,而不是您要設(shè)置的字符串文本userID
。
改成這樣:
userID.setText("" + session.getID());
或者
userID.setText(String.valueOf(session.getID()));
添加回答
舉報
0/150
提交
取消