Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findViewById returns null for preference layout

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 ?

like image 435
Adithya Avatar asked Mar 29 '13 17:03

Adithya


People also ask

What does findViewById return?

findViewById returns an instance of View , which is then cast to the target class. All good so far. To setup the view, findViewById constructs an AttributeSet from the parameters in the associated XML declaration which it passes to the constructor of View . We then cast the View instance to Button .

What does findViewById return if it doesn't find a view object with the matching ID?

. findViewById(R. id. retry) would always return null.

What does this method do findViewById?

FindViewById<T>(Int32) Finds a view that was identified by the id attribute from the XML layout resource.

What is Viewview findViewById?

view. findViewById() is used to find a view inside a specific other view. For example to find a view inside your ListView row layout. Your app crashes without it because the view you're searching for is inside rowView .


2 Answers

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

like image 192
Adithya Avatar answered Oct 30 '22 13:10

Adithya


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>
like image 30
ArkadiBernov Avatar answered Oct 30 '22 15:10

ArkadiBernov