1
votes

I created my login application using Volley, php, MYSQL and Shared preferences. Login and registration activities are successful, but pages do not go to other activities after login successful. Is it an Intent or another problem? Please help!

Login activity:

//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();
                        }
                    }
                }
3
You could try adding a flag to the intent. Intent intent = new Intent(LoginActivity.this, ProfileActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish();Deepak Unnikrishnan
Ok sir..i have do this..but still same..only toast message display..agazslee
Try logging a message just after the startActivity to make sure the line of code is executed. Also see if there are any exceptions in the LogCatDeepak Unnikrishnan

3 Answers

1
votes

Change this to

   //starting the profile activity

   finish();
   startActivity(new Intent(getApplicationContext(), ProfileActivity.class));

to

 startActivity(new Intent(LoginActivity.this, ProfileActivity.class));
 finish();
0
votes

The error should have been fixed by moving finish() to after you start the next activity. Because it wasn't I don't think you are actually getting to that line of code at all. You do get to the line where you set your toast, so your error must be between there and your startactivity line.

I think that it is likely you have an error in here:

                           JSONObject userJson = obj.getJSONObject("user");

                            //creating a new user object
                            User user = new User(
                                    userJson.getInt("id"),
                                    userJson.getString("fullname"),
                                    userJson.getString("phone")
                            );

The key -user, id, fullname, or phone- likely doesn't exist. And when you try to access them you're getting an error that's preventing your code from getting to the line where you change the activity. Check your Logcat or RUN consoles to see if my hunch is correct.

0
votes

Try using local context for example if you are in Mainactivity than Mainactivity.this and if you are in fragment try using getactivity(), I think it should work properly.