suannai
suannai

Reputation: 562

Extend Dialog goes wrong in android

I extend a Dialog

java code

public class Dialog_query extends Dialog implements OnClickListener {   

}  

Through the XML defines a interface enter image description here

Now want to select date by clicking on 日期1 The code is as follow Recorded as:code button

java button

Button btn=(Button)findViewById(R.id.BtnDate);           
        btn.setOnClickListener(new View.OnClickListener() {   
          public void onClick(View v) {   
            new DatePickerDialog(Dialog_query.this,   
                d2,   
                dateAndTime.get(Calendar.YEAR),   
                dateAndTime.get(Calendar.MONTH),   
                dateAndTime.get(Calendar.DAY_OF_MONTH)   
                ).show();   
          }   
        }); 

Now the question is,point out"The constructor DatePickerDialog(Dialog_query, DatePickerDialog.OnDateSetListener, int, int, int) is undefined"

Later found in here to add"Toast.makeText(LoginSuccess.this, "About agile software 1.0", Toast.LENGTH_LONG).show();"

also point out"The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (Dialog_query, String, int)"

But the "code button" is right in Activity.

Why in the extend cases will prompt such a mistake?

Upvotes: 3

Views: 658

Answers (1)

AlexMok
AlexMok

Reputation: 764

You're Dialog_query inheritates from dialog. But the Dialog class does not inheritate from Activity: http://developer.android.com/reference/android/app/Dialog.html

The constructor of DatePickerDialog needs a context or activity. Put the name of your activity instead of Dialog_query.this

new DatePickerDialog(ActivityName.this,   
                d2,   
                dateAndTime.get(Calendar.YEAR),   
                dateAndTime.get(Calendar.MONTH),   
                dateAndTime.get(Calendar.DAY_OF_MONTH)   
                ).show(); 

Upvotes: 5

Related Questions