Grumpy Cat Grumpy Cat - 11 months ago 46
Android Question

No errors but alert dialog not displaying

I have non-activity class MenuHandler to handle menu-related events,
I am trying to display developers message in alert dialog box, this message fetch from firebase real time database.

Everything is fine but alert dialog not displaying, I try debugger to check there any error in database but I got value properly from database. No Error to get value from database.

when I select menu from main activity, developerMessage toast shows then nothing happened.

I pass context to MenuHandler class properly.
Can I Show Alerdialog in using context?
or I need to write code only in main activity only(i.e. only activity class).

thanks in advance.


public void developersMessage()

if (isInternetOn()) {

Toast.makeText(mContext,"Loading message please wait",Toast.LENGTH_SHORT).show();

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();

mDatabase.child("version_1_0").child("dev").addListenerForSingleValueEvent(new ValueEventListener() {

public void onDataChange(DataSnapshot dataSnapshot) {

try {

final AlertDialog.Builder builder = new AlertDialog.Builder(mContext,;

catch (Exception e)

public void onCancelled(DatabaseError databaseError) {


Toast.makeText(mContext,"please turn on internet ",Toast.LENGTH_SHORT).show();

Answer Source

I got the answer, There nothing wrong in the code but bug lies in how I send context from mainactivity

here I wrongly init Menuhandler class

public boolean onOptionsItemSelected(MenuItem item) {

 final MenuHandler mMenuHandler = new MenuHandler(this.getApplicationContext());//bug lies here

if we are showing alertdialog then we need this way to pass context so our alertdialog display properly

public boolean onOptionsItemSelected(MenuItem item) {
        final MenuHandler mMenuHandler = new MenuHandler(MainActivity.this);//this is right way to pass context

hope this is helpful to someone in future.