findViewById returns null for preference layout

对着背影说爱祢 提交于 2019-12-08 18:23:12

问题


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>

which i am instantiating as follows : (in version 2.3.3)

addPreferencesFromResource(R.xml.responder_generic);

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>

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>

and

<?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>

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 ?


回答1:


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.

usage of findviewbyid in preferenceactivity




回答2:


The reason lies in using several PreferenceScreen. Each Preference view has its own id.

To access the view IDs, you need to create your own Preference class inheriting from Preference and override onBindView

public class LogListPreference extends EditTextPreference {

    public LogListPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LogListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setLayoutResource(R.layout.preference_widget_loglist);
    }

     @Override
        protected void onBindView(View view) {
            super.onBindView(view);
            EditText edit = (EditText) view.findViewById(R.id.logText);
            ...
        }
}

To use your class in PreferenceScreen xml-file, specify

 <PreferenceScreen
 xmlns:android="http://schemas.android.com/apk/res/android">

     <com.company.LogListPreference
         android:title="@string/pref_log_title_log"
         android:key="log_text" />

 </PreferenceScreen>

Preference Layout file /layout/preference_widget_loglist.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- Layout used by CheckBoxPreference for the checkbox style. This is inflated
     inside android.R.layout.preference. -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget_log_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="top|left"
    android:orientation="vertical"
    >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
        android:paddingRight="?android:attr/listPreferredItemPaddingRight">
        >

        <TextView
            android:id="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceListItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView
            android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:layout_below="@android:id/title"
            android:maxLines="2"
            android:paddingTop="6dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:textSize="14sp" />


    </RelativeLayout>


    <EditText
        android:id="@+id/logText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="false"
        android:defaultValue="@string/pref_default_video_sources"
        android:focusable="false"
        android:gravity="top|left"
        android:inputType="textMultiLine"
        android:key="log331"
        android:lines="10"
        android:maxLines="2000"
        android:minLines="6"
        android:scrollbars="vertical"
        android:scrollbarThumbVertical="?android:attr/colorBackground"
        android:background="?android:attr/colorForeground"
        android:textColor="?android:attr/textColorPrimaryInverse"
        android:text="aaa\nbbb\nccc\naaa\nbbb"
        android:title="@string/pref_title_video_sources" />

</LinearLayout>


来源:https://stackoverflow.com/questions/15708599/findviewbyid-returns-null-for-preference-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!