我创建了一个用于不同 Activity 的 AlertDialog fragment 。由于某种原因,警报不会被消除,并且主 Activity 中的代码不会继续。我输入了很多日志命令来查看它到底在哪里停止。以下是我的警报对话框:
public class Convert_units_dialog extends DialogFragment {
final static String TAG = "TAG_convert_units";
public static Convert_units_dialog newInstance() {
Convert_units_dialog frag = new Convert_units_dialog();
Bundle args = new Bundle();
args.putInt("title", R.string.change_units_dialog);
frag.setArguments(args);
return frag;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
return new AlertDialog.Builder(getActivity())
.setTitle(title)
.setPositiveButton(R.string.convert, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i(TAG, "inside click listener");
convert_units();
//dismiss dialog here
Log.i(TAG, "Start dismiss");
dismiss();
Log.i(TAG, "End dismiss");
}
})
.setNegativeButton(R.string.dont_convert,null)
.create();
}
在我的 Activity 中,我创建了一个调用alertDialog 的函数。
private void open_alert_dialog() {
DialogFragment newFragment = Convert_units_dialog.newInstance();
newFragment.show(getFragmentManager(), "dialog");
Log.i(TAG,"open_alert_dialog finished");
}
当我运行代码时,我在日志中看到“endmiss”,并且从未看到“open_alert_dialog finish”,因此我的 Activity 中遵循此命令的代码没有运行。我有什么明显做错的事情吗?我已经尝试了很多我见过的建议,但到目前为止没有一个有效。
我对一般编程有点陌生,希望得到任何意见。谢谢。
最佳答案
您的代码似乎是正确的,但请尝试一些操作,看看它是否可以解决:
- 检查 DialogFragment 和 AlertDialog 的导入,它们在您的 Activity 和 Convert_units_dialog 上应该相同。 (如果您使用的是 appcompat-v7 库,则会显示其他导入选项)
- 在 setPositiveButton 上尝试调用 dialog.dismiss(); 而不是 dismiss();
为了给您准确的答案,我们需要一些信息,例如:
- convert_units() 的实现是什么;
- 您在 Activity 的哪个位置调用 open_alert_dialog()
最后,检查文档,我相信您已经检查过它,但值得再读一遍:http://developer.android.com/reference/android/app/DialogFragment.html#AlertDialog
关于android - AlertDialog 没有被解雇。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30805303/