2012-01-03 1 views
3

dualtogglepreference라는 2 개의 토글이있는 사용자 정의 기본 설정 레이아웃을 만들었습니다. 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

NULL을 다음과 같이 가져 오는 중입니까? 나는 당신이 가진 문제를 정확히 파악할 수 없었다. 자세한 정보를 제공해주십시오. – kosa

+0

예 followMe이 (가) 올라오고 있습니다 – Leo

+0

DualTogglePreference 확장 기본 설정이 필요합니다. 아직이 링크를 참조하지 않을 수도 있습니다. http://www.java2s.com/Code/Android/Core-Class/AcustompreferencetypeTreference 참조의 수를 계산하여 기억 장치에 저장합니다. – kosa

답변

2

필자의 경우, 나는 inflator에 의해 사용될 생성자를 정의하는 것을 게을리했다.

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); 
    } 
}