findViewById在BottomSheetDialogFragment中返回null

问题描述 投票:-1回答:1

现在我正在使用BottomSheetDialogFragment。所以我想创建一个自定义按钮来关闭内部对话框,就像这样

<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>

然后我使用此方法显示底部对话框。

private void showFoodListFilterButton() {
        Button foodListFilterButton = (Button) getActivity().findViewById(R.id.food_filter_button);
            foodListFilterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tasteFilterBottomDialogFragment = TasteFilterBottomDialogFragment.newInstance();
                tasteFilterBottomDialogFragment.show(getActivity().getSupportFragmentManager(), "Taste");
                hideFoodListFilterButton();
            }
        });
    }

所以在那之后我试着通过这样调用dismiss()来解散它。

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_filter_close_button按钮然后我得到了这个error

'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

那么我怎么能解决这个问题或者我错过了什么?

谢谢!

android android-dialogfragment bottom-sheet
1个回答
2
投票

由于该按钮在onCreateView期间膨胀,您可以在onCreateView()期间设置监听器:

@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;
}

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

© www.soinside.com 2019 - 2024. All rights reserved.