此DialogFragment实现将导致
IllegalStateException(“您不能设置对话框的OnCancelListener或OnDismissListener")
。为什么?解决办法?
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");
}
}
}
我的活动实现DialogInterface.OnCancelListener
如下:
public class MainActivity extends Activity implements OkCancelDialogListener{
static final String TAG ="MainActivity";
@Override
public void onCancel(DialogInterface dialog) {
}
}
Exeception是从builder.create();
抛出的。怎么了?
发布于 2013-02-14 05:46:57
公共对话框onCreateDialog (Bundle savedInstanceState) 覆盖以生成您自己的自定义对话框容器。这通常用于显示AlertDialog而不是一般的对话框;在这样做时,不需要实现onCreateView(LayoutInflater、ViewGroup、Bundle),因为AlertDialog处理自己的内容。 此方法将在onCreate(Bundle)之后和onCreateView(LayoutInflater,ViewGroup,Bundle)之前调用。默认实现只是实例化并返回Dialog类。 备注: DialogFragment拥有Dialog.setOnCancelListener和Dialog.setOnDismissListener回调。你不能亲自设置它们。 要了解这些事件,请重写onCancel(DialogInterface)和onDismiss(DialogInterface).
因此,基本上,您必须重写onDismiss或OnCancel,而不是“.setOnCancelListener(OnCancelListener)”。
发布于 2015-07-20 06:07:36
你可以试试这个:
@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
}
希望能帮上忙..。
发布于 2013-01-25 03:16:02
您为对话框设置OnCancelListener,而不是为其构建器设置。
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;
}
https://stackoverflow.com/questions/14520597
复制相似问题