我使用Volley,php,MYSQL和Shared首选项创建了我的登录应用程序。登录和注册活动成功,但登录成功后页面不会转到其他活动。是意图还是其他问题?请帮忙!
登录活动:
//if everything is fine StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.URL_LOGIN, new Response.Listener() { @Override public void onResponse(String response) { progressBar.setVisibility(View.GONE); try { //converting response to json object JSONObject obj = new JSONObject(response); //if no error in response if (!obj.getBoolean("error")) { Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show(); //getting the user from the response JSONObject userJson = obj.getJSONObject("user"); //creating a new user object User user = new User( userJson.getInt("id"), userJson.getString("fullname"), userJson.getString("phone") ); //storing the user in shared preferences SharedPrefManager.getInstance(getApplicationContext()).userLogin(user); //starting the profile activity finish(); startActivity(new Intent(getApplicationContext(), ProfileActivity.class)); } else { // when error Toast.makeText(getApplicationContext(), "Ops! "+obj.getString("message"), Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } } }
将此更改为
//starting the profile activity
finish();
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
至
startActivity(new Intent(LoginActivity.this, ProfileActivity.class));
finish();
在开始下一个活动之后,应该通过将finish()移动到已修复的错误。因为它不是我认为你实际上根本没有达到那一行代码。您可以到达设置吐司的行,因此您的错误必须介于那里和您的起始行之间。
我认为你可能在这里遇到错误:
JSONObject userJson = obj.getJSONObject("user");
//creating a new user object
User user = new User(
userJson.getInt("id"),
userJson.getString("fullname"),
userJson.getString("phone")
);
密钥-user,id,fullname或phone-可能不存在。当您尝试访问它们时,您会收到一条错误,导致您的代码无法进入更改活动的行。检查Logcat或RUN控制台,看看我的预感是否正确。
尝试使用本地上下文,例如,如果你在Mainactivity而不是Mainactivity.this,如果你在片段尝试使用getactivity(),我认为它应该正常工作。