android - findViewById 在 BottomSheetDialogFragment 内返回 null
问题描述
现在我正在研究 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
那么我该如何解决这个问题,或者我错过了什么?
谢谢!
解决方案
由于该按钮在 期间膨胀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()
推荐阅读
- shell - 从多个文本文件中提取公共行并显示原始行号
- excel - 文本框日期值以变体编号显示,vba编码excel
- linux - Linux crontab 似乎无法正常工作
- python - Colab - python 代码中的 input() 框太长,所以我需要向后滚动阅读问题
- c# - EF Core 2.2.6 映射Oracle打包函数
- reactjs - 如何在 React 中为 MDBDataTable 设置自定义起始页码
- wordpress - 301 和 302 重定向混合 URL
- c# - 从 Elastic GET API 获取 top xyz
- angular - Angular 9:ANSI 编码的文本文件在使用 http.get 从 url 读取 .txt 时显示“for”(大引号)
- redux - 一种使用 createAsyncThunk 触发多个异步操作的简洁方法