开发者

What context do I use for a new Dialog?

I need a custom dialog to appear on 开发者_运维百科button press. Here is my code:

Button button3 = (Button) findViewById(R.id.Button03); 
    button3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        final Dialog custdialog = new Dialog(this);


        custdialog.setContentView(R.layout.custom_drink);

        custdialog.setTitle("Custom Drink");

        custdialog.setCancelable(false);

 /etc.

However, on this line: final Dialog custdialog = new Dialog(this);

I get an error saying "The constructor Dialog(new View.OnClickListener(){}) is undefined".

I've tried replacing "this" with other things, but with no luck. It's odd that I get an error on this line because I have the exact same thing (except different names and stuff) in my project, beneath a switch (like this:

@Override
 public boolean onOptionsItemSelected(MenuItem item) {
 switch (item.getItemId()) {
    case R.id.settings: 

 final Dialog settingsdialog = new Dialog(this);

    settingsdialog.setContentView(R.layout.custom_dialog);

    settingsdialog.setTitle("Settings");

    settingsdialog.setCancelable(false);

and that one works fine... hmmm.


First, on your actual question, instead of this, use MyActivity.this, where MyActivity is the name of the Activity class. You are trying to use this from an inner class, but the actual object you need is the outer class.

Also, never use getApplicationContext() for much of anything, particularly anything related to the UI.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜