1

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.

I have attached my code below

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()) && (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;
    }




}
Saraschandraa
  • 478
  • 1
  • 9
  • 27

1 Answers1

3

Change this line

 AlertDialog.Builder alt = new AlertDialog.Builder(getApplicationContext());

to

 AlertDialog.Builder alt = new AlertDialog.Builder(arg0.getContext());

and you should change arg0 to something meaningful like view or v. If that doesn't work then please post the logcat so we can see what error you are getting. You need to use the appropriate Context for the situation and here you want the AlertDialog to use the Activity Context which is the same Context that your Button (or arg0) uses.

Note MainActivity.this will do the same thing here as argo.getContext() but I was recently informed that it is bad practice for reasons such as if you want to re-use this code then you have to change the activity name part of the code. Its a less dynamic way of accessing the Context.

Here is a good SO answer that addresses the issue of Context. It can be a tricky concept to grasp at first so you may want to read over it a few times and keep it close by.

Community
  • 1
  • 1
codeMagic
  • 43,753
  • 13
  • 72
  • 91