Android 如何解决在setContentView()之后返回Null的findViewByID()问题
我在一个布局文件中有一个Android 如何解决在setContentView()之后返回Null的findViewByID()问题,android,android-dialog,Android,Android Dialog,我在一个布局文件中有一个按钮元素,add\u site\u bottom\u sheet.xml,id为@+id/add\u site\u sheet\u add\u site\u按钮 在我的SitesActivity.java中,在onCreate()中,我调用buildAddSiteSheet() 在这个方法中,我创建了一个BottomSheetDialog的实例,然后调用setContentView(R.layout.add\u site\u bottom\u sheet) 在setCon
按钮
元素,add\u site\u bottom\u sheet.xml
,id为@+id/add\u site\u sheet\u add\u site\u按钮
在我的SitesActivity.java
中,在onCreate()
中,我调用buildAddSiteSheet()
在这个方法中,我创建了一个BottomSheetDialog
的实例,然后调用setContentView(R.layout.add\u site\u bottom\u sheet)
在setContentView(…)
之后,我用findViewById(R.id.add\u site\u sheet\u add\u site\u Button)
分配Button addSiteButton
,但返回的是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.
}
});
}
}
添加站点\u底部\u 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
中推荐了一些方法,我认为这些方法与保持可读性完全无关,但是如果有人认为其中可能有什么内容,我很乐意编辑以包含其他方法
我还想补充,我已经尝试从add\u site\u bottom\u sheet.xml
获取所有其他元素,但是findViewById(…)
也会为这些元素返回null
我没有任何重复的布局,我也没有任何其他大小的布局,所以我不知所措 请使用
addSiteButton =(Button) addSiteSheet.findViewById(R.id.add_site_sheet_add_site_button);
它应该是
addSiteButton=addSiteSheet.findViewById(R.id.add\u site\u sheet\u add\u site\u按钮)代码>@ADM它总是那么简单。。。我只是没有发现自己处于这样的境地,我不得不这样做。谢谢大家的关注!这是因为您不知道#findViewById()
方法是否有效。在使用任何新东西之前阅读。@ADM这是一个公平的观点,应该是这样的addSiteButton=addSiteSheet.findViewById(R.id.add\u site\u sheet\u add\u site\u button)代码>