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:
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");
}
}
}
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 throws an exception as "Demo - Demo Exception".
I am not sure but i think the exception raises from getApplicationContext().
Update:- 1
The exception which i am getting, as follows:
Update:- 2 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.
Please help me and catch me out...
thanx