1
if (Commons.HaveNetworkConnection()) {
                if ((txt_USERNAME.getText().toString().trim() != null && txt_USERNAME
                        .getText().toString().length() != 0)// username
                                                            // check
                        && (txt_PASSWORD.getText().toString().trim() != null && txt_PASSWORD
                                .getText().toString().length() != 0)) {// password
                    // check
                    pd = ProgressDialog.show(LoginActivity.this.getApplicationContext(), "","Please wait...");
                    Toast.makeText(getContext(), "data"+txt_USERNAME + txt_PASSWORD, Toast.LENGTH_LONG).show();
                    Thread thread = new Thread(LoginActivity.this);
                    thread.start();

                } else {
                    Toast.makeText(getBaseContext(),
                            "Invalid username or password",
                            Toast.LENGTH_LONG).show();
                }
            } else {
                Commons.setOnlineAlert();
            }

}catch(Exception e){Toast.makeText(getBaseContext(),"problem in onclick 1 block"+e,Toast.LENGTH_LONG).show();}
        }

        private Context getContext() {
            // TODO Auto-generated method stub
            return null;
        }

    });

when i click on login button then runtime exception occur

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 

Please help me how we can solve this problem

flx
  • 14,146
  • 11
  • 55
  • 70

2 Answers2

1

your are showing toast message with null.. Remove this method

 private Context getContext() {
            // TODO Auto-generated method stub
            return null;
        }

and in this line

Toast.makeText(getContext(), "data"+txt_USERNAME + txt_PASSWORD, Toast.LENGTH_LONG).show();

change to

Toast.makeText(getApplicationContext(), "data"+txt_USERNAME + txt_PASSWORD, Toast.LENGTH_LONG).show();
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
0

In your Context method you should actually return the context of activity not null

    private Context getContext() {
        // TODO Auto-generated method stub
        return LoginActivity.this.getApplicationContext();
    }

And set this in your Toast message as below:

      Toast.makeText(getContext(), "data"+txt_USERNAME + txt_PASSWORD, Toast.LENGTH_LONG).show();
GrIsHu
  • 29,068
  • 10
  • 64
  • 102