0

I want to insert an alert dialog into my android app. It's fine when I follow the tutorial to add it in a simple project, which only has a MainActivity.java, activity_main.xml and a layout xml.

However, when I want to insert the code into my project, I get some questions. My code is:

android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(this)
                        .setTitle("Exit?")
                        .setMessage("Are you sure want to QUIT ?")
                        .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                pmaControllerManager.OnExit();
                            }
                        })
                        .setNegativeButton("no", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int i) {
                                dialog.cancel();
                            }
                        })
                        .setNeutralButton("cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.cancel();
                            }
                        })
                        .show();

There is a warning symbol in the this in the first line. How could I fix it? p.s. below is the function where I call showDialog(I call it in buttons[5]):

private void setbutton()
{
    buttons[0]=(ImageButton)menu.findViewById(R.id.button0);
    buttons[1]=(ImageButton)menu.findViewById(R.id.button1);
    buttons[2]=(ImageButton)menu.findViewById(R.id.button2);
    buttons[3]=(ImageButton)menu.findViewById(R.id.button3);
    buttons[4]=(ImageButton)menu.findViewById(R.id.button4);
    buttons[5]=(ImageButton)menu.findViewById(R.id.button5);

    buttons[1].setBackgroundResource(R.drawable.screen);
    buttons[2].setBackgroundResource(R.drawable.sketch);
    buttons[3].setBackgroundResource(R.drawable.importdraw);
    buttons[4].setBackgroundResource(R.drawable.setting);
    buttons[5].setBackgroundResource(R.drawable.out);
    center.setBackgroundResource(R.drawable.noticeicon_using);

    /*center.setOnClickListener(new ImageButton.OnClickListener(){
        @Override
        public void onClick(View v) {
            if(click)
            {
                closemenu();
                //mode=false;
            }
            else
            {
                openmenu();
                //mode=true;
            }
            click=!click;
        }
    });*/
    center.setOnTouchListener(new View.OnTouchListener() {
        boolean isMove=false;
        int touchy,touchx,y;
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            int action=motionEvent.getAction();
            if(action==MotionEvent.ACTION_DOWN)
            {
                y=params.y;
                //Log.i("y:",String.valueOf(y));
                touchy=(int)motionEvent.getRawY();
                touchx=(int)motionEvent.getRawX();
            }
            else if(action==MotionEvent.ACTION_MOVE)
            {
                if(Math.abs(motionEvent.getRawX()-touchx)<30)
                {
                    isMove=true;
                    params.gravity=Gravity.RIGHT;
                    params.y=y+(int)motionEvent.getRawY()-touchy;
                    wm.updateViewLayout(center,params);
                    wm.updateViewLayout(menu,params);
                }
            }
            else if(action==MotionEvent.ACTION_UP)
            {
                if(!isMove)
                {
                    if(click)
                    {
                        closemenu();
                        //mode=false;
                    }
                    else
                    {
                        openmenu();
                        //mode=true;
                    }
                    click=!click;
                }
                else
                    isMove=false;

            }
            return true;
        }
    });

    buttons[1].setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_DOWN)
                buttons[1].setBackgroundResource(R.drawable.screen_focus);
            else if(event.getAction()==MotionEvent.ACTION_UP)
            {
                buttons[1].setBackgroundResource(R.drawable.screen);
                pmaControllerManager.newTask("screen");
            }
            return true;
        }
    });
    buttons[2].setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_DOWN)
                buttons[2].setBackgroundResource(R.drawable.sketch_focus);
            else if(event.getAction()==MotionEvent.ACTION_UP)
            {
                buttons[2].setBackgroundResource(R.drawable.sketch);
                pmaControllerManager.newTask("sketch");
            }
            return true;
        }
    });
    buttons[3].setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_DOWN)
                buttons[3].setBackgroundResource(R.drawable.importdraw_focus);
            else if(event.getAction()==MotionEvent.ACTION_UP)
            {
                buttons[3].setBackgroundResource(R.drawable.importdraw);
            }
            return true;
        }
    });
    buttons[4].setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_DOWN)
                buttons[4].setBackgroundResource(R.drawable.setting_focus);
            else if(event.getAction()==MotionEvent.ACTION_UP)
            {
                buttons[4].setBackgroundResource(R.drawable.setting);
                pmaControllerManager.Setting();
            }
            return true;
        }
    });
    buttons[5].setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction()==MotionEvent.ACTION_DOWN)
                buttons[5].setBackgroundResource(R.drawable.exit_focus);
            else if(event.getAction()==MotionEvent.ACTION_UP)
            {
                buttons[5].setBackgroundResource(R.drawable.out);
                shoaDialog(menuwindow.this);
                pmaControllerManager.OnExit();
            }
            return true;
        }
    });
}
Ron
  • 1
  • 1

4 Answers4

2

Pass Context instead of this because this is a reference to the current object. It means if you call it for example from lambda this will be a reference of object which is extended from anonymous class.

so you can make method like

public void showDialog(Context context) {
    AlertDialog alartDialog = AlertDialog.Builder(context).
           ...
           .show();
}

and then call it like

showDialog(MainActivity.this); //for activity
showDialog(MainFragment.this.getActivity()); //for fragment

By the way better to make something like Util class for it:

class DialogUtil {
   public static void showDialog(Context context) {
             AlertDialog alartDialog = AlertDialog.Builder(context).
           ...
           .show();
   }
}

Then call it like

DialogUtil.showDialog(MainActivity.this); //for activity
DialogUtil.showDialog(MainFragment.this.getActivity()); //for fragment

UPDATE

Instead of calling it like:

shoaDialog(menuwindow.this);

You can pass Context as I described above or you can get it from View:

buttons[5].setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    //some code here
        showDialog(v.getContext());
        return true;
    }
});
Andrey Danilov
  • 6,194
  • 5
  • 32
  • 56
  • Thank you for your answer. So, I have created a new method but I cannot call **showDialog()** in other function. Is it correct to call it in other function? – Ron Feb 08 '18 at 09:14
  • @Ron Make it static. So you can call it from everywhere. I will give example – Andrey Danilov Feb 08 '18 at 09:51
  • @Ron just wrap it like this. And yeah its correct to call in other functions. – Andrey Danilov Feb 08 '18 at 09:56
  • Unfortunately, it still doesn't work. It is fine when I make the method with or without **static**. However, I just cannot call it in other function. p.s. I've edited my post and add where I call it. THX. – Ron Feb 08 '18 at 10:14
0

replace this to MainActivity.this like :

android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(MainActivity.this)

Nikunj
  • 3,937
  • 19
  • 33
0

Replace this with MainActivity.this or getApplicationContext()

android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(MainActivity.this)

If you are using dialog look below :

For Activity : ActivityName.this

android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(MainActivity.this)

For Fragment : getActivity()

 android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(getActivity())

For Adapter : Context context;

android.app.AlertDialog alartDialog = new android.app.AlertDialog.Builder(context)

Also, Look into this Difference between getContext() , getApplicationContext() , getBaseContext() and "this"

Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44
0

The AltertDialog requires a context and you are passing it the activity. Try using

new android.app.AlertDialog.Builder(this.getApplicationContext())

instead.

Codendaal
  • 514
  • 6
  • 14