DialogFragment:button.setOnClickListener 抛出 NullPointerException

DialogFragment: button.setOnClickListener throws NullPointerException

我有一个 Activity 按钮是这样的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="onButtonClickListener"
    android:text="Show Fragment" />
</RelativeLayout>

我在 xml 中的 onButtonClickListener 方法是(这里我创建了 DialogFragment 并设置了 onClickListener 用于对话框中的清除按钮):

 public void onButtonClickListener(View view) {
    FragmentManager fm = getFragmentManager();
    MyDialogFragment dialogFragment = new MyDialogFragment ();
    dialogFragment.show(fm, "Sample Fragment");

    final EditText messageText = (EditText) findViewById(R.id.editText);
    Button clearButton = (Button) findViewById(R.id.button_clear);

   clearButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            messageText.setText(null);
        }
    });
}

我的问题是当 DialogFragment 显示时抛出 NullPointerException:

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.cristi.dialogfragmenttest2.MainActivity.onButtonClickListener(MainActivity.java:51)

在这行代码:clearButton.setOnClickListener(new View.OnClickListener() {

我的 DialogFragment xml 是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:maxLength="200" />

<Button
    android:id="@+id/button_clear"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Clear"
    android:layout_below="@+id/editText"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

setOnClickListener 可能有什么问题?为什么它说我的 DialogFragment 按钮为空?

欢迎任何参考或建议!提前致谢!

编辑:

-MyDialogFragment代码:

public class MyDialogFragment extends DialogFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.dialog_fragment, container, false);
    getDialog().setTitle("Enter message");
    return rootView;
}

您必须引用您的按钮 button_clear 所在的根视图,然后调用 rootView.findViewById(...) 获取按钮引用... 解决方案:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.dialog_fragment, container, false);
        getDialog().setTitle("Enter message");
     Button clearButton = (Button) rootView.findViewById(R.id.button_clear);

       clearButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                messageText.setText(null);
            }
        });
        return rootView;
    }