-1

I have a Button element in a Layout file, add_site_bottom_sheet.xml with the id @+id/add_site_sheet_add_site_button.

In my SitesActivity.java, in onCreate(), I call buildAddSiteSheet().

In this method, I create an instance of a BottomSheetDialog, then I call setContentView(R.layout.add_site_bottom_sheet).

After setContentView(...), I assign Button addSiteButton with findViewById(R.id.add_site_sheet_add_site_button) but this is returning null.

I've looked in multiple places and cannot find any answers at the moment.

I was wondering if you lovely people would have a spy at my code to see if I'm missing anything.

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>

I've ommited some methods in SitesActivity.java that I think are completely unrelated to keep things readable on SO, but if anyone thinks there might be anything there, I'll happily edit to include the other methods.

I would also like to add, I've tried getting all other elements from add_site_bottom_sheet.xml, but findViewById(...) returns null for those too.

I don't have any duplicate layouts and I don't have any other sizes of layouts yet either so I'm at a loss!

ADM
  • 20,406
  • 11
  • 52
  • 83
whiteside0013
  • 33
  • 1
  • 8

1 Answers1

0

Please use

addSiteButton =(Button) addSiteSheet.findViewById(R.id.add_site_sheet_add_site_button);
Shanto George
  • 994
  • 13
  • 26
  • it should be like this `addSiteButton =addSiteSheet.findViewById(R.id.add_site_sheet_add_site_button);` – AskNilesh Jan 09 '19 at 04:32