目前我正在研究显示对话框的片段活动,但是我真的不明白为什么我不能显示我的对话框。下面是我在android开发者页面上使用的示例:
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.dialog_fire_missiles)
.setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}并使用此命令调用该对话框:
public void confirmFireMissiles() {
DialogFragment newFragment = new mainActivity();
newFragment.show(getSupportFragmentManager(), "missiles");
}现在getSupportFragmentManager总是变成红色,当我关注它时,它会给我一个错误,告诉我无法解析方法'show(?,java.lang.String)‘和无法解析方法'getSupportFragmentManager()’。我现在处于恐慌状态,我不知道该怎么做。我已经导入了所需的内容,但我不知道为什么还会发生这种情况。
发布于 2013-06-11 15:50:40
试试下面的方法。
比方说点击按钮。
DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(getFragmentManager(), "missiles");编辑:
如果您的最小sdk低于api 11,请导入以下内容。扩展FragmentActivity
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;使用
DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(getSupportFragmentManager(), "missiles"); https://stackoverflow.com/questions/17038741
复制相似问题