问题描述
此DialogFragment实现导致
This DialogFragment implementation causes an
.为什么?解决方案?
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
,如下所示:
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) {
}
}
从builder.create();
引发欺骗.怎么了?
推荐答案
来自 Android文档:
重写以构建自己的自定义Dialog容器.这是通常用于显示AlertDialog而不是通用对话框;这样做时,onCreateView(LayoutInflater,ViewGroup,Bundle)可以无需实施,因为AlertDialog会自行处理内容.
Override to build your own custom Dialog container. This istypically used to show an AlertDialog instead of a generic Dialog;when doing so, onCreateView(LayoutInflater, ViewGroup, Bundle) doesnot need to be implemented since the AlertDialog takes care of its owncontent.
此方法将在onCreate(Bundle)之后和之前调用onCreateView(LayoutInflater,ViewGroup,Bundle).默认值实现只是实例化并返回一个Dialog类.
This method will be called after onCreate(Bundle) and beforeonCreateView(LayoutInflater, ViewGroup, Bundle). The defaultimplementation simply instantiates and returns a Dialog class.
注意:DialogFragment拥有Dialog.setOnCancelListener和Dialog.setOnDismissListener回调.您不能自己设置它们.
要了解有关这些事件的信息,请重写onCancel(DialogInterface)和onDismiss(DialogInterface).
To find out about these events, override onCancel(DialogInterface) andonDismiss(DialogInterface).
因此,基本上,您必须覆盖onDismiss或OnCancel而不是'.setOnCancelListener(onCancelListener)'.
So basically, you must override the onDismiss or OnCancel instead of '.setOnCancelListener(onCancelListener)'.
这篇关于IllegalStateException(“您不能设置Dialog的OnCancelListener或OnDismissListener")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!