9

次のような設定画面 (responder_generic.xml) があります。

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

addPreferencesFromResource(R.xml.responder_generic);

そして私のレイアウト=preference_header_switch_item_responderは次のようになります:

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

layout今、私はフォルダで定義された複合ボタンをlayout/v-14それぞれ次のように持っています:

<?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を返します。

理由は何ですか?

4

2 に答える 2

7

理由は、画面に複数の設定を追加していたため、カスタム設定を追加する必要がありました。詳細な解決策は、次のリンクで提供されています。

解決策は、次のリンクで提供されています。

設定アクティビティでの findviewbyid の使用

于 2013-04-02T12:03:45.320 に答える