我试图在android studio中创建一个对话警报。但这里显示代码无法访问。请帮帮我。
public class Mydialog extends DialogFragment
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder theDialog = new AlertDialog.Builder(getActivity());
theDialog.setTitle("Sample Dialog");
theDialog.setMessage("hello world");
return super.onCreateDialog(savedInstanceState);
theDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
toasty("Clicked Ok");
}
});
}
public void toasty(String str)
{
Toast.makeText(getActivity(),str,Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:1)
嗯,那是因为你在方法结束前return super.onCreateDialog(savedInstanceState);
。将此行放在onCreateDialog()
。
修改强>
抱歉,我的意思是删除这一行并添加到最后:
return theDialog.create();