Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IllegalStateException( "You can not set Dialog's OnCancelListener or OnDismissListener")

This DialogFragment implementation causes an

IllegalStateException( "You can not set Dialog's OnCancelListener or OnDismissListener")

. Why? Solution?

public class OkCThreadDialog1 extends DialogFragment{  DialogInterface.OnCancelListener onCancelListener;  public OkCThreadDialog1(){ }  public static OkCThreadDialog1 newInstance(String title, String message) {     OkCThreadDialog1 frag = new OkCThreadDialog1();     Bundle args = new Bundle();     args.putString("title", title);     args.putString("message", message);     frag.setArguments(args);     return frag; }   public Dialog onCreateDialog(Bundle savedInstanceState){      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());       builder .setTitle(getArguments().getString("title"))             .setMessage(getArguments().getString("message"))             .setOnCancelListener(onCancelListener)             .setPositiveButton("Ok", new DialogInterface.OnClickListener() {                 public void onClick(DialogInterface dialog, int id) {                 }})             .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                 public void onClick(DialogInterface dialog, int id) {                     getDialog().cancel();                 }});      return builder.create(); }  @Override public void onAttach(Activity activity) {     super.onAttach(activity);     // Verify that the host activity implements the callback interface     try {         onCancelListener = (DialogInterface.OnCancelListener) activity;     } catch (ClassCastException e) {         // The activity doesn't implement the interface, throw exception         throw new ClassCastException(activity.toString()                 + " must implement OkCancelDialogListener");     } } } 

My Activity implements DialogInterface.OnCancelListener as follow:

public class MainActivity extends Activity implements OkCancelDialogListener{  static final String TAG ="MainActivity";  @Override public void onCancel(DialogInterface dialog) {   } } 

Exeception is thrown from builder.create();. What's wrong?

like image 702
user1709805 Avatar asked Jan 25 '13 11:01

user1709805


2 Answers

From Android documentation:

public Dialog onCreateDialog (Bundle savedInstanceState)

Override to build your own custom Dialog container. This is typically used to show an AlertDialog instead of a generic Dialog; when doing so, onCreateView(LayoutInflater, ViewGroup, Bundle) does not need to be implemented since the AlertDialog takes care of its own content.

This method will be called after onCreate(Bundle) and before onCreateView(LayoutInflater, ViewGroup, Bundle). The default implementation simply instantiates and returns a Dialog class.

Note: DialogFragment own the Dialog.setOnCancelListener and Dialog.setOnDismissListener callbacks. You must not set them yourself.

To find out about these events, override onCancel(DialogInterface) and onDismiss(DialogInterface).

So basically, you must override the onDismiss or OnCancel instead of '.setOnCancelListener(onCancelListener)'.

like image 154
marius bardan Avatar answered Oct 13 '22 03:10

marius bardan


You may try this:

@Override public Dialog onCreateDialog(Bundle savedInstanceState) {     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());     builder.setTitle("Title");     builder.setMessage("Message");     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {         @Override         public void onClick(DialogInterface dialog, int which) {             // do something positive         }     });     return builder.create(); }  @Override public void onCancel(DialogInterface dialog) {     // do something } 

Hope it can help ...

like image 28
Peter Tschudin Avatar answered Oct 13 '22 05:10

Peter Tschudin