我创建了一个对话框,但在我的主要 Activity 的 on create 方法中无法显示它。
我正在像这样在对象上调用 show
:
FireMissilesDialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show();
但我收到一条错误消息,提示它无法解析方法“show”
这是我的对话代码(我正在学习如何制作它们,所以这是来自 android 开发站点对话培训的副本)
package com.shush;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
public class FireMissilesDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.name)
.setPositiveButton(R.string.name, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.address, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
最佳答案
代替
FireMissilesDialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show();
使用:
DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(getSupportFragmentManager(), "firemissile");
关于java - 无法显示安卓对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29760695/