title: Android - Getting Exception
body: <p>I am developing one application in which i am getting an exception, and i know this one is the silly or small mistake which i am doing but your help may catch me out and make my day:</p>
  
  <pre><code>public class Demo extends Activity
  {
      Button btnDemo;
          Thread t;
      AlertDialog alertDialog;
  
       @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.demo);
  
                btnDemo = (Button) findViewById(R.id.btnDemo);
            btnDemo.setOnClickListener(new OnClickListener() {
                        public void onClick(final View v) {
  
                            t=new Thread() {
                                 public void run() {
                                      tryDemo();
                                 }
                           };
                           t.start(); 
                        }
                  });
       }
  
      public void tryDemo()
      {
  
            try
           {
                int i = 5;
  
                if(i == 0 || i == 1)
                {
                      Intent intent_success = new Intent(getApplicationContext(), Main_Activity.class);
                      startActivity(intent_success);
                }
                else
                {
                  alertDialog = new  AlertDialog.Builder(getApplicationContext()).create();
                  alertDialog.setTitle("Demo");
                  alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int which) {
                         //here you can add functions
                  } }); 
                  alertDialog.setIcon(R.drawable.icon);
                  alertDialog.setMessage("Sorry!! Either Username or Password Invalid");
                  alertDialog.show();
  
                }
           }
           catch(Exception e)
           {
                 Log.i("Demo", "Demo - Demo Exception");
           }
      }   
  }
  </code></pre>
  
  <p>In above code, if i make i=0 or i=1 in tryDemo function then it is running successfully , but if i make it other than 0 or 1 then it <strong>throws an exception as "Demo - Demo Exception"</strong>.</p>
  
  <p>I am not sure but i think the exception raises from <strong><em>getApplicationContext()</em></strong>.</p>
  
  <p><strong>Update:- 1</strong></p>
  
  <p>The exception which i am getting, as follows:</p>
  
  <p><img src="https://i.stack.imgur.com/1ggK5.png" alt="alt text"></p>
  
  <p><strong>Update:- 2</strong>
  If I remove the "thread" part and wrote the whole function code in the button click event and replaced the "getApplicationContext()" with v.getContext() then it is running successfully.........but i want to implement it within the THREAD.</p>
  
  <p>Please help me and catch me out...</p>
  
  <p>thanx</p>