I am trying to setup an activity and when I try to assign some of my views to names in the .java file its telling that R.layout.bathing does not have field datePicker1 and it does have it. Here is my two code snippets. What should I change to get this to work?
我正在尝试设置一个活动,当我尝试将我的一些视图分配给.java文件中的名称时,它会告诉我们。洗澡没有野外数据表,它确实有。这是我的两个代码片段。我该怎么做才能让它生效?
bathing.java
bathing.java
setContentView(R.layout.bathing);
setTitle("Please Select Bathing Options");
bathdate = (DatePicker)findViewById(R.layout.bathing.datePicker1);
bathtime = (TimePicker)findViewById(R.layout.bathing.timePicker1);
add = (Button)findViewById(R.layout.bathing.button1);
bathing.xml
bathing.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="@string/bath_tracking"
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView3"
android:layout_marginTop="16dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/datePicker1"
android:layout_marginTop="51dp"
android:text="@string/date"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/datePicker1"
android:layout_below="@+id/datePicker1" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/timePicker1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="50dp"
android:text="@string/time"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/timePicker1"
android:orientation="vertical" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Yes" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_below="@+id/radioGroup1"
android:text="@string/No" />
</RadioGroup>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/radioGroup1"
android:layout_alignParentLeft="true"
android:layout_marginBottom="36dp"
android:text="@string/hair"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/timePicker1"
android:layout_alignParentRight="true"
android:layout_below="@+id/radioGroup1"
android:inputType="textMultiLine" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:text="@string/Add"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:text="@string/comment"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
1 个解决方案
#1
4
You have to access view objects via their id, instead of
您必须通过它们的id访问视图对象,而不是。
bathdate = (DatePicker)findViewById(R.layout.bathing.datePicker1);
use
使用
bathdate = (DatePicker)findViewById(R.id.datePicker1);
and so on. IDs are global, they are not specific to a certain layout.
等等。IDs是全局的,它们不是特定于某个布局的。
That doesn't neccessarily mean that each view has to have an unique name. The most important thing to know is that findViewById()
only searches in the view hierachy of the current activity (you can try this by calling it before setContentView()
- it will always return null). In the end it means that IDs should be unique in a single layout file.
这并不意味着每个视图都必须有唯一的名称。要知道的最重要的一点是,findViewById()只在当前活动的视图层次中搜索(您可以在setContentView()之前调用它,它总是返回null)。最后,这意味着id在一个布局文件中应该是唯一的。
There is no problem in having the same id in multiple layouts. Often it's even useful. Consider having an activity with a normal layout in res/layout/main.xml
and a different layout for the landscape format in res/layout-land/main.xml
. Android will manage these layouts for you. If you give views with identical purposes a different, unique id, you would have to do something along this in your code (pseudocode example):
在多个布局中拥有相同的id是没有问题的。经常甚至有用。考虑有一个正常布局的活动在res/layout/main。xml和在res/layout-land/main.xml中对景观格式的不同布局。Android会为你管理这些布局。如果您以相同的目的给视图一个不同的、惟一的id,那么您将不得不在代码中做一些事情(伪代码示例):
if(layout == portrait)
view = findViewById(R.id.viewid_portrait);
else if(layout == landscape)
view = findViewById(R.id.viewid_landscape);
If both have the same ID you won't have a conflict, but can access both via a single line call of findViewById()
.
如果两者都有相同的ID,那么就不会出现冲突,但是可以通过findViewById()的单行调用来访问它们。
#1
4
You have to access view objects via their id, instead of
您必须通过它们的id访问视图对象,而不是。
bathdate = (DatePicker)findViewById(R.layout.bathing.datePicker1);
use
使用
bathdate = (DatePicker)findViewById(R.id.datePicker1);
and so on. IDs are global, they are not specific to a certain layout.
等等。IDs是全局的,它们不是特定于某个布局的。
That doesn't neccessarily mean that each view has to have an unique name. The most important thing to know is that findViewById()
only searches in the view hierachy of the current activity (you can try this by calling it before setContentView()
- it will always return null). In the end it means that IDs should be unique in a single layout file.
这并不意味着每个视图都必须有唯一的名称。要知道的最重要的一点是,findViewById()只在当前活动的视图层次中搜索(您可以在setContentView()之前调用它,它总是返回null)。最后,这意味着id在一个布局文件中应该是唯一的。
There is no problem in having the same id in multiple layouts. Often it's even useful. Consider having an activity with a normal layout in res/layout/main.xml
and a different layout for the landscape format in res/layout-land/main.xml
. Android will manage these layouts for you. If you give views with identical purposes a different, unique id, you would have to do something along this in your code (pseudocode example):
在多个布局中拥有相同的id是没有问题的。经常甚至有用。考虑有一个正常布局的活动在res/layout/main。xml和在res/layout-land/main.xml中对景观格式的不同布局。Android会为你管理这些布局。如果您以相同的目的给视图一个不同的、惟一的id,那么您将不得不在代码中做一些事情(伪代码示例):
if(layout == portrait)
view = findViewById(R.id.viewid_portrait);
else if(layout == landscape)
view = findViewById(R.id.viewid_landscape);
If both have the same ID you won't have a conflict, but can access both via a single line call of findViewById()
.
如果两者都有相同的ID,那么就不会出现冲突,但是可以通过findViewById()的单行调用来访问它们。