title: I want to add alert dialog within if else statement in android
body: <p>I want to alert dialog to display if the password and login id doesn't match. I tried the following code, but when i run if the text are same then it executes, but in the case if the password and Login Id doesn't match an alert is supposed to be display but instead the process exits saying unfortunately your project is ended.</p>
  
  <p>I have attached my code below</p>
  
  <pre><code>package com.example.explicitintent;
  
  import java.security.PublicKey;
  
  import android.os.Bundle;
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
  
  public class MainActivity extends Activity {
      Button b1, b2,b3;
      EditText e1, e2;
      String username="saras", password="greek";
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          e1 = (EditText) findViewById(R.id.editText0001);
          e2 = (EditText) findViewById(R.id.editText0002);
          b1 = (Button) findViewById(R.id.button0002);
          b1.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View arg0) 
              {
                  // TODO Auto-generated method stub
                  if (username.equals(e1.getText().toString()) &amp;&amp; (password.equals(e2.getText().toString())))
                  {
                      Intent b = new Intent(MainActivity.this,Contacts.class);
                      String s = e1.getText().toString();
                      String s1 = e2.getText().toString();
                      b.putExtra("d1", s);
                      b.putExtra("d2", s1);
                      startActivity(b);
                  }
                  else
                  {
  
                      AlertDialog.Builder alt = new AlertDialog.Builder(getApplicationContext()); 
  
                      alt.setIcon(R.drawable.ic_launcher);
                      alt.setTitle("WARNING");
                      alt.setMessage("Do u want to re-enter password");
                      alt.setPositiveButton("YES", new DialogInterface.OnClickListener() 
                      {
  
                          @Override
                          public void onClick(DialogInterface arg0, int arg1)
                          {
                              Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_SHORT).show();
  
                          }
                      });
  
                      alt.setNegativeButton("NO",new DialogInterface.OnClickListener()
                      {
  
                          @Override
                          public void onClick(DialogInterface arg0, int arg1) 
                          {
                              Toast.makeText(getApplicationContext(),"OK", Toast.LENGTH_SHORT).show();
  
                          }
                      });
                      alt.show();
  
                  }
                  }
  
  
          });
  
          b2 = (Button) findViewById(R.id.button0003);
          b2.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View arg0) {
                  // TODO Auto-generated method stub
                  Intent c = new Intent(MainActivity.this, Reset.class);
                  startActivity(c);
              }
          });
  
          b3 = (Button) findViewById(R.id.button1);
          b3.setOnClickListener(new OnClickListener() 
          {
  
              @Override
              public void onClick(View arg0) 
              {
                  Toast.makeText(getApplicationContext(),"Password Saved", Toast.LENGTH_LONG).show();
  
              }
          });
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
  
  
  
  }
  </code></pre>