我在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" />
也許你因爲這個而變得空了? http://stackoverflow.com/a/5704613/858626 – 2014-06-26 00:05:36