无法显示android对话框

时间:2015-04-21 00:18:11

标签: java android layout dialog

我创建了一个对话框,但在主要活动的创建方法中显示它时遇到了问题。 我在这个对象上调用show

FireMissilesDialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show();

但我收到错误消息can't resolve method 'show'

这是我的Dialog代码(我正在学习如何制作它们,所以这是Android dev网站对话培训的副本)

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();
    }
}

1 个答案:

答案 0 :(得分:1)

而不是

  FireMissilesDialogFragment newFragment = new FireMissilesDialogFragment();
  newFragment.show();

使用:

  DialogFragment newFragment = new FireMissilesDialogFragment();
  newFragment.show(getSupportFragmentManager(), "firemissile");