问题描述
我有一个preference屏幕(responder_generic.xml)如下:
I have a preference screen (responder_generic.xml) as follows:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<Preference
android:defaultValue="false"
android:key="auto_responder_key"
android:layout="@layout/preference_header_switch_item_responder"
android:title="@string/auto_responder">
</Preference>
</PreferenceScreen>
对此我实例如下:(2.3.3版本)
which i am instantiating as follows : (in version 2.3.3)
addPreferencesFromResource(R.xml.responder_generic);
和我的布局= preference_header_switch_item_responder
看起来如下:
and my layout = preference_header_switch_item_responder
looks as follows:
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout of a header item in PreferenceActivity. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="33dp"
android:gravity="center_vertical"
android:minHeight="33dp"
android:id="@+id/preference_header_switch_item_responder"
style="?android:attr/listSeparatorTextViewStyle" >
<RelativeLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:singleLine="true"
android:text="@string/auto_responder"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
android:id="@+id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
<include layout="@layout/compound_button" />
</LinearLayout>
现在,我有中定义的化合物,按钮布局
文件夹和布局/ V-14
如下分别为:
Now, i have the compound button defined in layout
folder and layout/v-14
respectively as follows :
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<CheckBox
android:id="@+id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:focusable="false"
android:padding="8dip"
android:layout_marginRight="14dip" />
</merge>
和
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Switch
android:id="@+id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:focusable="false"
android:padding="8dip"
android:layout_marginRight="14dip"/>
</merge>
在 onPostCreate
的方法,我想找到使用 findViewById(R.id.switchWidget)在此复选框/在运行时切换
,但它总是返回null。
In the onPostCreate
method, i am trying to find the this checkbox/switch at run time using findViewById(R.id.switchWidget)
but its always returning null.
任何想法可能是什么原因?
Any idea what could be the reason ?
推荐答案
原因是我是添加多个preferences到屏幕上,因此,我不得不求助于添加自定义preference。详细的解决方案已经在以下链接提供了:
Reason was i was adding multiple preferences to the screen, hence, i had to resort to adding a custom preference. Detailed solution has been provided in the following link:
该解决方案在下面的链接被提供。
The solution has been provided in the following link.
<一个href=\"http://stackoverflow.com/questions/15728192/usage-of-findviewbyid-in-$p$pferenceactivity\">usage findviewbyid在preferenceactivity
这篇关于findViewById返回null preference布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!