我正在尝试为我的 Android 4.0+ 应用程序建立一个设置屏幕。我无法获得两个复选框vibratePreference
并soundPreference
显示。我认为UserPreferencesFragment
永远不会实例化。以下是涉及的文件。我已经为此工作了几个晚上,但我无法弄清楚我错过了什么。我在看什么?
谢谢,解放军
首选项_headers.xml
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" >
<header
android:fragment="com.hometelco.hfa.activities.UserPreferencesFragment"
android:summary="Home Field Advantage user settings"
android:title="User Settings" />
</preference-headers>
首选项.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="User Settings" >
<CheckBoxPreference
android:defaultValue="true"
android:key="vibratePreference"
android:summary="Vibrate on adds and updates"
android:title="Vibrate" />
<CheckBoxPreference
android:defaultValue="true"
android:key="soundPreference"
android:summary="Play sound on adds and updates"
android:title="Sound" />
</PreferenceCategory>
</PreferenceScreen>
UserPreferencesActivity.java
package com.hometelco.hfa.activities;
import java.util.List;
import android.preference.PreferenceActivity;
import android.util.Log;
import com.hometelco.hfa.R;
public class UserPreferencesActivity extends PreferenceActivity {
private static final String TAG = "UserPreferencesActivity";
public void onBuildHeaders(List<Header> target) {
Log.i(TAG, "onBuildHeaders before load");
loadHeadersFromResource(R.xml.preference_headers, target);
Log.i(TAG, "onBuildHeaders after load");
}
}
UserPreferencesFragment.java
package com.hometelco.hfa.activities;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.util.Log;
import com.hometelco.hfa.R;
public class UserPreferencesFragment extends PreferenceFragment {
private final static String TAG = "UserPreferencesFragment";
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "UserPreferencesFragment onCreate");
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}