2013-01-25 17 views
33

thực hiện DialogFragment Điều này gây ra một IllegalStateExceptionIllegalStateException ("Bạn không thể đặt OnCancelListener Dialog hoặc OnDismissListener")

("Bạn không thể đặt OnCancelListener Dialog hoặc OnDismissListener")

. Tại sao? Dung dịch?

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"); 
    } 
} 
} 

Hoạt động của tôi thực hiện DialogInterface.OnCancelListener như sau:

public class MainActivity extends Activity implements OkCancelDialogListener{ 

static final String TAG ="MainActivity"; 

@Override 
public void onCancel(DialogInterface dialog) { 


} 
} 

Exeception được ném từ builder.create();. Chuyện gì vậy?

Trả lời

84

Từ tài liệu Android:

công Dialog onCreateDialog (Bundle savedInstanceState)

Override để xây dựng của bạn tùy chỉnh hộp thoại Dialog. Đây là thường được sử dụng để hiển thị AlertDialog thay vì Hộp thoại chung; khi làm như vậy, onCreateView (LayoutInflater, ViewGroup, Bundle) không không cần phải được triển khai kể từ khi AlertDialog tự xử lý nội dung của riêng nó.

Phương thức này sẽ được gọi sau onCreate (Bundle) và trước onCreateView (LayoutInflater, ViewGroup, Bundle). Việc thực thi mặc định đơn giản là khởi tạo và trả về một hộp thoại Dialog.

Lưu ý: DialogFragment sở hữu các cuộc gọi lại Dialog.setOnCancelListener và Dialog.setOnDismissListener. Bạn không được tự đặt chúng.

Để tìm hiểu về các sự kiện này, hãy ghi đè lênHủy (DialogInterface) và onDismiss (DialogInterface).

Vì vậy, về cơ bản, bạn phải ghi đè lên onDismiss hoặc OnCancel thay vì '.setOnCancelListener (onCancelListener)'.

+1

Cảm ơn vì điều này. Đây sẽ là câu trả lời được chấp nhận. –

0

Bạn đặt OnCancelListener cho hộp thoại không dành cho trình tạo của nó.

public Dialog onCreateDialog(Bundle savedInstanceState){ 

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder .setTitle(getArguments().getString("title")) 
     .setMessage(getArguments().getString("message")) 
     .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(); 
      }}); 

    Dialog dialog = builder.create(); 
    dialog.setOnCancelListener(onCancelListener); 
    return dialog; 
} 
+2

chỉ cố gắng .. nó không tác phẩm nào! – user1709805

+0

Điều này cũng không có tác dụng với tôi. Tôi chỉ sử dụng giải pháp trong câu trả lời được chấp nhận. – Micer

-3

Hãy thử mã này tôi hy vọng đây sẽ là công việc ..

AlertDialog.Builder builder = new AlertDialog.Builder(
      class_name.this); 
    builder.setTitle("Title");  
    AlertDialog dialog = builder.create(); 
    dialog.setButton("ok", new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int which) {    

    }); 


    dialog.setButton2("Cancel", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 


      dialog.cancel(); 

     } 
    }); 
    dialog.show(); 
4

Bạn có thể thử này:

@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 
} 

Hy vọng nó có thể giúp ...

Các vấn đề liên quan