0

AlertDialog.Builder builder1 = new AlertDialog.Builder(getBaseContext());

OR

AlertDialog.Builder builder1 = new AlertDialog.Builder(context);

OR

AlertDialog alertDialog = new AlertDialog.Builder(this).create();

Any of above code does not work if I write this code in any child class of main activity class, application get crashed.

If I write this code under onCreate method, then it works fine.

2 Answers2

0

Giving an Activity should work:

new AlertDialog.Builder(YourActivity.this).create();
shkschneider
  • 17,833
  • 13
  • 59
  • 112
0

Try This Code :

Activity.runOnUiThread(new Runnable() {
                            public void run() {
                                    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                            }
                        });