2013-04-10 27 views
0

我在SharedPreferences佈局文件中獲得了ImageView我想要獲取引用,以便可以在PrefsActivity類中創建偵聽器。到目前爲止,我還沒有運氣得到一個使用findViewById的引用,所有東西都返回爲null。在PreferenceActivity創建的佈局中獲取對按鈕的引用

的設置是這樣的:

PrefsActivity.class

public class PrefsActivity extends UnifiedSherlockPreferenceActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // Set header resource MUST BE CALLED BEFORE super.onCreate 
     setHeaderRes(R.xml.pref_headers); 
     // Set desired preference file and mode (optional) 
     setSharedPreferencesName("my_prefs"); 
     setSharedPreferencesMode(Context.MODE_PRIVATE); 

     super.onCreate(savedInstanceState); 
     ACRA.init(getApplication()); 

     getSupportActionBar().setDisplayShowCustomEnabled(true); 
     getSupportActionBar().setIcon(R.drawable.new); 
     getSupportActionBar().setDisplayShowTitleEnabled(false); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

pref_headers.xml

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:unified="http://schemas.android.com/apk/res-auto" > 

<header 
    unified:fragment="com.my.app.PrefsActivity$GeneralPreferenceFragment" 
    unified:title="@string/pref_header_general" 
    unified:preferenceRes="@xml/pref_general" /> 

pref_general.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 

<PreferenceCategory 
android:title="Category Title" 
android:layout="@layout/settings_message" 

<EditTextPreference 
    android:capitalize="words" 
    android:defaultValue="" 
    android:inputType="textCapWords" 
    android:key="first_name" 
    android:maxLines="1" 
    android:selectAllOnFocus="true" 
    android:singleLine="true" 
    android:title="@string/first_name" /> 

settings_message.xml

這包含按鈕btnTutorial我試圖去

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 


<TextView 
    android:id="@+id/tvSettingsMessage" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="20px" 
    android:text="@string/settings_text" /> 

<Button 
    android:id="@+id/btnTutorial" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="20px" 
    android:text="@string/settings_button_text" /> 

+0

也許你因爲這個而變得空了? http://stackoverflow.com/a/5704613/858626 – 2014-06-26 00:05:36

回答

0

你缺少的參考以下方法在您的onCreate() ...

setContentView(R.layout.yourlayout); 

...所以返回空值。嘗試實施這些更改,然後讓我們知道它是否有效。

+0

感謝您輸入Karan。沒有setContentView()是因爲我假設UnifiedSherlockPreferenceActivity使用R.xml.pref_headers即時創建視圖。雖然我注意到我已經錯過了我現在更新的一個xml文件 – KingFu 2013-04-10 20:45:18

+0

@KingFu:thnx通知我...... – 2013-04-10 20:57:31

0

也許你可以從方法訪問您的ImageView 'onPreferenceTreeClick',如:

@Override 
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 
     Preference preference) { 
    super.onPreferenceTreeClick(preferenceScreen, preference); 
    if (preference != null && preference.getKey().equals("imageViewKey")) { 
     // do something here 
    } 
    return false; 
} 
0

這裏是解決方案:

在你settings_message.xml集屬性附加傷害的按鈕

安卓的onClick =「onLogoutClick」

In activity PrefsActivity define method public void onLogoutClick(View v){}

你準備好了! :)

0

用途:

Preference myPreference = findPreference(key); 

,其中關鍵是在你的XML的關鍵,例如, 「FIRST_NAME」。

相關問題