1
m.example.arihant.pludoc E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.arihant.pludoc, PID: 9852
  java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{2ed82f1e V.E..... R......D 0,0-1026,348} not attached to window manager
      at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:396)
      at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:322)
      at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)
      at android.app.Dialog.dismissDialog(Dialog.java:341)
      at android.app.Dialog.dismiss(Dialog.java:324)
      at com.example.arihant.pludoc.Login.hideDialog(Login.java:213)
      at com.example.arihant.pludoc.Login.access$400(Login.java:42)
      at com.example.arihant.pludoc.Login$5.onErrorResponse(Login.java:186)
      at com.android.volley.Request.deliverError(Request.java:524)
      at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
      at android.os.Handler.handleCallback(Handler.java:739)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5300)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
02-01 00:01:25.547 9852-9852/com.example.arihant.pludoc D/AppTracker: App Event: crash

CODE:

public class Login extends AppCompatActivity  {

    private static final String[] DUMMY_CREDENTIALS = new String[]{
            "foo@example.com:hello", "bar@example.com:world"
    };
    private static final String TAG ="" ;

    private UserLoginTask mAuthTask = null;
    private EditText mPasswordView;
    private View mProgressView;
    private View mLoginFormView;
    private EditText mPhoneView;
    private TextView mForgotPass;
    private SessionManager session;
    private SQLiteHandler db;
    private ProgressDialog pDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        setupActionBar();
        pDialog = new ProgressDialog(this);
        pDialog.setCancelable(false);
        db = new SQLiteHandler(getApplicationContext());
        session = new SessionManager(getApplicationContext());
        mPasswordView =(EditText)findViewById(R.id.password);
        mPhoneView = (EditText)findViewById(R.id.phone);
        mForgotPass = (TextView)findViewById(R.id.forgotpass);

        if (session.isLoggedIn()) {

            Intent intent = new Intent(Login.this, Mainpage.class);
            startActivity(intent);
            finish();
        }

        mForgotPass.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {

            }
        });

        Button mEmailSingUpButton = (Button)findViewById(R.id.sign_up_button);
        mEmailSingUpButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
               Intent i1 = new Intent(Login.this,Signup.class);
                startActivity(i1);
            }
        });

        Button mEmailSignInButton = (Button) findViewById(R.id.sign_in_button);
        mEmailSignInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String phone = mPhoneView.getText().toString().trim();
                String password = mPasswordView.getText().toString().trim();

                if (!phone.isEmpty() && !password.isEmpty()) {
                    attemptLogin();
                    checkLogin(phone , password);
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Please enter the credentials!", Toast.LENGTH_LONG)
                            .show();
                }
            }
        });

        mLoginFormView = findViewById(R.id.login_form);
        mProgressView = findViewById(R.id.login_progress);
    }

    private void checkLogin(final String phone, final String password) {
        // Tag used to cancel the request
        String tag_string_req = "req_login";

        pDialog.setMessage("Logging in ...");
        showDialog();


        StringRequest strReq = new StringRequest(Method.POST,
                AppConfig.URL_LOGIN, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Login Response: " + response.toString());
                hideDialog();

                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");


                    if (!error) {

                        session.setLogin(true);


                        String uid = jObj.getString("uid");

                        JSONObject user = jObj.getJSONObject("user");
                        String name = user.getString("name");
                        String email = user.getString("email");
                        String phone = user.getString("phone");
                        String created_at = user
                                .getString("created_at");


                        db.addUser(name,email, phone, uid, created_at);


                        Intent intent = new Intent(Login.this,
                                Mainpage.class);
                        startActivity(intent);
                        finish();
                    } else {

                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getApplicationContext(),
                                errorMsg, Toast.LENGTH_LONG).show();
                    }
                } catch (JSONException e) {

                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "Login Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_LONG).show();
                hideDialog();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                // Posting parameters to login url
                Map<String, String> params = new HashMap<String, String>();
                params.put("phone", phone);
                params.put("password", password);

                return params;
            }

        };

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    }

    private void showDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hideDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }



    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }


    private void attemptLogin() {
        if (mAuthTask != null) {
            return;
        }

        // Reset errors.
        mPhoneView.setError(null);
        mPasswordView.setError(null);



        String phone = mPhoneView.getText().toString();
        String password = mPasswordView.getText().toString();

        boolean cancel = false;
        View focusView = null;


        if (TextUtils.isEmpty(phone)) {
            mPhoneView.setError(getString(R.string.error_field_required));
            focusView = mPhoneView;
            cancel = true;
        } else if (isPhoneValid(phone)) {
            mPhoneView.setError(getString(R.string.error_invalid_phone));
            focusView = mPhoneView;
            cancel = true;
        }
        else if (TextUtils.isEmpty(password)) {
            mPasswordView.setError(getString(R.string.error_field_required));
            focusView = mPasswordView;
            cancel = true;
        }
        else if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
            mPasswordView.setError(getString(R.string.error_invalid_password));
            focusView = mPasswordView;
            cancel = true;
        }




        if (cancel) {
            focusView.requestFocus();
        } else {
            showProgress(true);
            mAuthTask = new UserLoginTask(phone, password);
            mAuthTask.execute((Void) null);
        }
    }



    private boolean isPhoneValid(String phone){
        return phone.length()!=10;
    }
    private boolean isPasswordValid(String password) {
        return password.length() > 4;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    private void showProgress(final boolean show) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);

            mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
            mLoginFormView.animate().setDuration(shortAnimTime).alpha(
                    show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
                }
            });

            mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
            mProgressView.animate().setDuration(shortAnimTime).alpha(
                    show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
                }
            });
        } else {
            mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
            mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
        }
    }



    public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {

        private final String mPhone;

        private final String mPassword;

        UserLoginTask(String phone, String password) {
            mPhone = phone;
            mPassword = password;
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            // TODO: attempt authentication against a network service.

            try {
                // Simulate network access.
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                return false;
            }

            for (String credential : DUMMY_CREDENTIALS) {
                String[] pieces = credential.split(":");
                if (pieces[0].equals(mPhone)) {
                    // Account exists, return true if the password matches.
                    return pieces[1].equals(mPassword);
                }
            }

            // TODO: register the new account here.
            return true;
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            mAuthTask = null;
            showProgress(false);

            if (success) {
                finish();
            } else {
                mPasswordView.setError(getString(R.string.error_incorrect_password));
                mPasswordView.requestFocus();
            }
        }

        @Override
        protected void onCancelled() {
            mAuthTask = null;
            showProgress(false);
        }
    }
}
George Mulligan
  • 11,813
  • 6
  • 37
  • 50
Arihant Dugar
  • 31
  • 1
  • 9
  • someone please help me with this thing – Arihant Dugar Jan 31 '16 at 18:42
  • I have seen this before when you try and dismiss a dialog after the activity has already been destroyed. Although I'm not sure this is happening in your case see [here](http://stackoverflow.com/a/23586127/1435985) for a possible solution. – George Mulligan Jan 31 '16 at 18:45
  • have seen this but dont get whats wrong, it was working all fine suddenly i dont know what happened – Arihant Dugar Jan 31 '16 at 18:49
  • Is your `Login` activity running when you try and dismiss the dialog (`onResume` has been called and the activity has not been paused)? – George Mulligan Jan 31 '16 at 18:54
  • when i try to login it shows dialog and then crashes – Arihant Dugar Jan 31 '16 at 18:56
  • In your `UserLoginTask` you call `finish();`. If that occurs before you dismiss the dialog that is likely your problem. You can try temporarily removing `finish` from there to check if that is your problem and fix your logic if that works. – George Mulligan Jan 31 '16 at 19:08
  • okay will try thank you! – Arihant Dugar Jan 31 '16 at 19:27
  • it doesnt work :( the toast error is also not displayed here now :( – Arihant Dugar Jan 31 '16 at 19:30
  • So it still crashes? I'll have to take a closer look later then. – George Mulligan Jan 31 '16 at 19:45
  • `attemptLogin();` looks like it should be used for testing while `checkLogin(phone , password);` looks like the real method. I don't see why you would want to run both of these methods at once. It still looks like your problem was the `finish()` call in `onPostExecute()` to me. You can try removing the call to `attemptLogin();` altogether since you aren't testing. – George Mulligan Jan 31 '16 at 22:21
  • Still doesnot work :( please help me – Arihant Dugar Feb 01 '16 at 16:07
  • You need to give more to go on than "Still doesn't work". Is it still crashing with the same error message? Has the stack trace in the error message changed at all? – George Mulligan Feb 01 '16 at 16:09
  • doesnot crash but i cannot see the toast message at all. the toast message is blank – Arihant Dugar Feb 01 '16 at 16:16
  • Which toast message are you trying to see? Does the ProgressDialog go away correctly now? – George Mulligan Feb 01 '16 at 16:18
  • yes it goes away correctly but i cannot see the toast message inside check login – Arihant Dugar Feb 01 '16 at 16:19
  • There are several toast methods in that method. Which one in particular? Are you seeing your log messages to logcat? – George Mulligan Feb 01 '16 at 16:23
  • log cat doesnot show anything . and none of the toast are working – Arihant Dugar Feb 01 '16 at 16:26
  • i think it is not able to fetch from the php file and the toast is there but shows blank toast – Arihant Dugar Feb 01 '16 at 16:27
  • You are probably getting to this statement then ` Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();`. Your error message must be blank from the server. Try adding some text to it and see if you see it then. – George Mulligan Feb 01 '16 at 16:31
  • yes i tried that it is unable to fetch from localhost . any tips for how to check the connection ? i have set all thing right in my appconfig file to link it to server. and also the ip address is correct with the port of mamp – Arihant Dugar Feb 01 '16 at 16:33
  • Open chrome and manually browse to the page or try it from another computer. Maybe you need to open the port in your firewall for other machines to connect to it. Also make sure you are using the ip address of the server. – George Mulligan Feb 01 '16 at 16:37
  • okay will see . can you send me a link if you know the best one ? thanks a lot for all the help :) made my day – Arihant Dugar Feb 01 '16 at 16:42
  • I don't have a link because I don't know how your environment is setup. It should be easy enough though to make sure your server is accessible from your test device or another computer and to test a request without using your application to make sure everything is accessible and working as expected. – George Mulligan Feb 01 '16 at 16:45
  • okay will try thanks – Arihant Dugar Feb 01 '16 at 17:26
  • Possible duplicate of [View not attached to window manager crash](http://stackoverflow.com/questions/22924825/view-not-attached-to-window-manager-crash) – Richard Le Mesurier Nov 08 '16 at 09:04
  • Please refer [this](http://stackoverflow.com/a/41033320/6422096) link it may help you accordingly! – Satish Silveri Dec 08 '16 at 07:25

0 Answers0