2012-01-03 81 views
3

我做了一個自定義首選項佈局,每行有2個切換,稱爲dualtogglepreference。連同一個擴展Preference的類一起處理它的一些細節。當我將這個自定義首選項添加到我的preferences.xml文件時,它出現在UI中,但我無法在首選項活動中使用findPreference來引用它。在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

您是否將followingMe作爲NULL?我無法準確理解你所遇到的問題。請提供更多信息。 – kosa 2012-01-03 20:05:29

+0

是的followingMe即將到來null – Leo 2012-01-03 20:23:00

+0

我希望你的DualTogglePreference擴展偏好。可能是你可以參考這個鏈接,如果還沒有http://www.java2s.com/Code/Android/Core-Class/Acustompreferencetype這個偏好的數量單擊它已經收到並且存儲從這個收藏中收回.htm – kosa 2012-01-03 20:50:09

回答

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); 
    } 
} 
相關問題