Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否在显示后调用onCreateDialog?_Android_Android Layout_Android Fragments_Android Alertdialog - Fatal编程技术网

Android 是否在显示后调用onCreateDialog?

Android 是否在显示后调用onCreateDialog?,android,android-layout,android-fragments,android-alertdialog,Android,Android Layout,Android Fragments,Android Alertdialog,我正在尝试实现自定义的对话框片段。但是当我试图显示它时,我得到了NullPointerException。我还注意到,onCreateDialog从未被隐式调用。 有什么问题吗。我已经阅读了官方手册,并遵循其中的所有步骤 这是我自定义对话框片段的代码 public class UserInputDialogFragment extends DialogFragment { InputDialogListener mListener; private EditText mText

我正在尝试实现自定义的
对话框片段
。但是当我试图显示它时,我得到了
NullPointerException
。我还注意到,
onCreateDialog
从未被隐式调用。
有什么问题吗。我已经阅读了官方手册,并遵循其中的所有步骤
这是我自定义对话框片段的代码

 public class UserInputDialogFragment extends DialogFragment {
    InputDialogListener mListener;
    private EditText mTextEdit;

    public UserInputDialogFragment() {
        super();
    }

    // Use this instance of the interface to deliver action events

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        View mainView = inflater.inflate(R.layout.dialog_input, null);
        builder.setView(mainView);
        mTextEdit = (EditText) mainView.findViewById(R.id.user_input);
        if (mTextEdit==null) {
            Log.e("ERROR","Text edit is null");
        }
        // Add action buttons
        builder.setPositiveButton(R.string.ok_btn, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                mListener.onDialogPositiveClick(UserInputDialogFragment.this,mTextEdit.getText().toString());
            }
        });
        builder.setNegativeButton(R.string.cancel_bnt, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                mListener.onDialogNegativeClick(UserInputDialogFragment.this,mTextEdit.getText().toString());
                UserInputDialogFragment.this.getDialog().cancel();
            }
        });

        return builder.create();
    }

    public interface InputDialogListener {
        public void onDialogPositiveClick(DialogFragment dialog, String userInput);

        public void onDialogNegativeClick(DialogFragment dialog, String userInput);
    }


    public void showAndAddHint(FragmentManager manager,String tag,String hint) {
        this.onCreateDialog(null);
        mTextEdit.setHint(hint);
        this.show(manager,tag);

    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // Verify that the host activity implements the callback interface
        try {
            // Instantiate the NoticeDialogListener so we can send events to the host
            mListener = (InputDialogListener) activity;
        } catch (ClassCastException e) {
            // The activity doesn't implement the interface, throw exception
            throw new ClassCastException(activity.toString()
                    + " must implement InputDialogListener");
        }
    }
}
我试着用这种方式展示对话

 UserInputDialogFragment userInputDialogFragment = new UserInputDialogFragment();
                    userInputDialogFragment.showAndAddHint(getFragmentManager(),"Please enter phone number",task.phoneNumber);
这里是
NullPointerException
mTextEdit
null

public void showAndAddHint(FragmentManager manager,String tag,String hint) {
    this.onCreateDialog(null);
    mTextEdit.setHint(hint);
    this.show(manager,tag);

}

showAndAddHint
方法无法按编写的方式工作。你应该做的是:

1-设置成员变量
mHint=hint

2-按您现在的方式调用
show()

3-在“创建”对话框中读取成员变量
mHint
,并使用它设置编辑文本提示


不要显式调用
onCreateDialog
,因为show方法会在需要时为您执行此操作。

工作起来很有魅力。谢谢。新年快乐