如何解决在 setContentView() 之后返回 Null 的 findViewByID()

How to resolve findViewByID() returning Null after setContentView()

我在布局文件中有一个 Button 元素,add_site_bottom_sheet.xml,id 为 @+id/add_site_sheet_add_site_button

在我的SitesActivity.java中,在onCreate()中,我调用buildAddSiteSheet()

在这个方法中,我创建了一个 BottomSheetDialog 的实例,然后调用 setContentView(R.layout.add_site_bottom_sheet).

setContentView(...) 之后,我将 Button addSiteButton 赋值给 findViewById(R.id.add_site_sheet_add_site_button),但这会返回 null

我查看了多个地方,目前找不到任何答案。

我想知道你们这些可爱的人是否会监视我的代码,看看我是否遗漏了什么。

SitesActivity.java


    public class SitesActivity extends AppCompatActivity {

        ITrapLossCalculator calc;
        ICustomer customer;
        Controller controller;

        RecyclerView recyclerView;
        SitesRecyclerViewAdapter recyclerAdapter;
        RecyclerView.LayoutManager recyclerManager;
        FloatingActionButton fab;
        BottomSheetDialog addSiteSheet;

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.sites_screen);

            buildAppBar();           //method is omitted from this class

            Intent intent = getIntent();

            controller = intent.getParcelableExtra(CustomersActivity.EXTRA_CONTROLLER);
            calc = controller.getLossCalculator();
            customer = intent.getParcelableExtra(EXTRA_CUSTOMER);

            setTitle(customer.getName());

            buildRecyclerView();    //method is omitted from this class
            buildFAB();             //method is omitted from this class
            buildAddSiteSheet(); 

        }

        private void buildAddSiteSheet(){
            Button addSiteButton;
            addSiteSheet = new BottomSheetDialog(this);
            addSiteSheet.setContentView(R.layout.add_site_bottom_sheet);

            // addSiteButton below is null.
            addSiteButton = findViewById(R.id.add_site_sheet_add_site_button);

            addSiteButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //TODO - Callback to add site to model needs to be implemented.
                }
            });
        }

    }

add_site_bottom_sheet.xml


    <?xml version="1.0" encoding="utf-8"?>

    <GridLayout
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/BoilerAndValve"
        android:background="@color/colorBandVAccent"
        android:columnCount="2"
        android:rowCount="4">

            <EditText
                android:id="@+id/add_site_sheet_address_line_1_entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:layout_column="0"
                android:layout_row="0"
                android:layout_columnWeight="1"
                android:hint="@string/add_new_site_sheet_address_line_1_hint"
                android:autofillHints="no"
                tools:targetApi="o" />

            <EditText
                android:id="@+id/add_site_sheet_address_line_2_entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:layout_column="0"
                android:layout_row="1"
                android:layout_columnWeight="1"
                android:hint="@string/add_new_site_sheet_address_line_2_hint"
                android:autofillHints="no"
                tools:targetApi="o"/>

            <EditText
                android:id="@+id/add_site_sheet_address_line_3_entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:layout_column="0"
                android:layout_row="2"
                android:layout_columnWeight="1"
                android:hint="@string/add_new_site_sheet_address_line_3_hint"
                android:autofillHints="no"
                tools:targetApi="o"/>

            <EditText
                android:id="@+id/add_site_sheet_postcode_entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:layout_column="0"
                android:layout_row="3"
                android:layout_columnWeight="1"
                android:hint="@string/add_new_site_sheet_postcode_hint"
                android:autofillHints="no"
                tools:targetApi="o"/>

            <EditText
                android:id="@+id/add_site_sheet_name_entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:layout_column="1"
                android:layout_row="0"
                android:layout_columnWeight="1"
                android:hint="@string/add_new_site_sheet_name_hint"
                android:autofillHints="no"
                tools:targetApi="o"/>


            <EditText
                android:id="@+id/add_site_sheet_phone_entry"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="phone"
                android:layout_column="1"
                android:layout_row="1"
                android:layout_columnWeight="1"
                android:hint="@string/add_new_site_sheet_phone_hint"
                android:autofillHints="no"
                tools:targetApi="o"/>

            <Button
                android:id="@+id/add_site_sheet_add_site_button"
                android:layout_column="1"
                android:layout_row="3"
                android:layout_columnWeight="1"
                android:text="@string/add_site_button_text"/>


        </GridLayout>

我在 SitesActivity.java 中省略了一些方法,我认为这些方法与保持 SO 上的可读性完全无关,但如果有人认为那里可能有任何东西,我会很乐意编辑以包括其他方法.

我还想补充一点,我已经尝试从 add_site_bottom_sheet.xml 中获取所有其他元素,但是 findViewById(...) returns 对于这些元素也为 null。

我没有任何重复的布局,我也没有任何其他尺寸的布局,所以我很茫然!

请使用

addSiteButton =(Button) addSiteSheet.findViewById(R.id.add_site_sheet_add_site_button);