2012-01-03 8 views
3

私は、デュアルトググルリファレンスと呼ばれる行ごとに2つのトグルを持つカスタムプリファレンスレイアウトを作成しました。 Preferenceを拡張したクラスに加えて、その特質のいくつかを扱います。このカスタムプリファレンスをpreferences.xmlファイルに追加すると、UIに表示されますが、プリファレンスアクティビティでfindPreferenceを使用して参照することができません。Can not find PreferenceActivityのカスタムプリファレンスのリファレンス

あるpreferences.xmlファイル

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
<PreferenceCategory 
    android:title="Notifications"> 

    <com.hitpost.testing.DualTogglePreference 
     android:key="followsMe" 
     android:title="Someone follows me" 
     android:layout="@layout/dualtogglepreference"/> 

</PreferenceCategory> 
</PreferenceScreen> 

PreferenceActivity

public class TestingCustomPreferenceActivity extends PreferenceActivity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    addPreferencesFromResource(R.xml.preferences); 

    DualTogglePreference followsMe = (DualTogglePreference) findPreference("followsMe"); 

    if (followsMe != null) 
     Log.e("FOLLOWS_ME", "NOT NULL"); 
    else 
     Log.e("FOLLOWS_ME", "NULL"); //THIS IS PRINTED 
} 
} 

ウィジェットのレイアウトが正しいこと、すなわち、視覚的にすべてが、完璧に見えます。助けてください、最後の日のためにこれと戦っている。

+0

NULLを次のように取得していますか?私はあなたが持っている問題を正確に把握できませんでした。詳細をご記入ください。 – kosa

+0

ありがとうございました。私はあなたのDualTogglePreference拡張設定を願っています。 – Leo

+0

まだリンクしていない場合は、このリンクを参照することができます。http://www.java2s.com/Code/Android/Core-Class/Acustompreferencetypeこのリファレンスは、受信したクリエイティブの数をカウントし、ストレージから取得します。 – kosa

答えて

2

私の場合、私はインフレータによって使用されるコンストラクタを定義することを怠っていました。

public class StaticDialogPreference extends DialogPreference { 
    // this constructor is called by the infaltor 
    public StaticDialogPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public StaticDialogPreference(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    private void init() { 
     setDialogMessage(getContext().getString(R.string.static_message)); 
     setNegativeButtonText(null); 
    } 
} 
関連する問題