Android BottomSheetDialogFragment中的findViewById返回null

Android BottomSheetDialogFragment中的findViewById返回null,android,android-dialogfragment,bottom-sheet,Android,Android Dialogfragment,Bottom Sheet,现在,我正在研究底部碎片。 所以我想创建一个自定义按钮来关闭对话框内部,如下所示 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="

现在,我正在研究底部碎片。 所以我想创建一个自定义按钮来关闭对话框内部,如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/level_12"
    android:paddingRight="@dimen/level_12"
    android:paddingBottom="@dimen/level_20">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end">

        <TextView
            android:id="@+id/taste_filter_close_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:padding="@dimen/level_10"
            android:text="@string/bottom_close_button" />
    </RelativeLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/basic_taste_filter_title"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/level_10"
        android:text="@string/basic_taste_filter_subtitle_default" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="horizontal"
        android:weightSum="1">

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:paddingStart="@dimen/level_flat"
            android:paddingLeft="@dimen/level_flat"
            android:paddingEnd="@dimen/level_2"
            android:paddingRight="@dimen/level_2">

            <Button
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/green_filter_button_bg"
                android:drawableTop="@drawable/ic_filter"
                android:paddingTop="@dimen/level_8"
                android:paddingBottom="@dimen/level_8"
                android:text="@string/sweet"
                android:textColor="@color/green_filter_button_text" />
        </FrameLayout>
    </LinearLayout>
</LinearLayout>
因此,在这之后,我尝试通过如下方式在其内部调用
disclose()
来消除它

public class TasteFilterBottomDialogFragment extends BottomSheetDialogFragment {

    public static TasteFilterBottomDialogFragment newInstance() {
        return new TasteFilterBottomDialogFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bottom_filter_dialog, container, false);
        // get the views and attach the listener
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        TextView closeBtn = (TextView) getActivity().findViewById(R.id.taste_filter_close_button);
        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
}
但是当我点击
taste\u filter\u close\u按钮
时,我得到了这个
错误

'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
那么,我该如何解决这个问题,或者我遗漏了什么


谢谢

由于该按钮在
onCreateView
期间充气,因此您可以在
onCreateView()期间设置侦听器:

请注意,我正在执行
view.findViewById()
而不是
getActivity().findViewById()

'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.bottom_filter_dialog, container, false);

    // get the views and attach the listener

    TextView closeBtn = (TextView) view.findViewById(R.id.taste_filter_close_button);
    closeBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    });

    return view;
}