0

Can someone tell me why cannot i call setOnClickListener method on my buton in custom dialog method? Is there a possible way to handle clicks in this kind of dialog? Here's the code:

private Dialog wifiDialog() { final Dialog dialog = new Dialog(activity); dialog.setContentView(R.layout.wifi_dialog); dialog.setTitle("Upload"); Button no = (Button) findViewById(R.id.button_wifi_No); Button yes = (Button) findViewById(R.id.button_wifi_Yes); no.setOnClickListener(new OnClickListener() { // runtimeException @Override public void onClick(View v) { wifiDialog().dismiss(); } }); return dialog; } 

Thanks in advance

1 Answer 1

3

Try this.

private Dialog wifiDialog(Context context) { final Dialog dialog = new Dialog(context); LayoutInflater layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = layoutInflater.inflate(R.layout.wifi_dialog,null); dialog.setContentView(view); dialog.setTitle("Upload"); Button no = (Button) view.findViewById(R.id.button_wifi_No); Button yes = (Button) view.findViewById(R.id.button_wifi_Yes); no.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); return dialog; } 

And use :

wifiDialog(this); // In activity context; 

I explain you why you getting error. You cycle your logic. In onClick method you try to create another dialog and set same click listener to the same button. SO if you want to dismiss your dialog use the instanse of dialog, instead of creation new.

Sign up to request clarification or add additional context in comments.

2 Comments

Please don't just post code with no explanation of what you changed and why, or the OP (and future visitors) learn nothing.
@Simon Sorry. I will take that into consideration

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.