我正在尝试为Android 4.0+应用建立设置屏幕。我无法显示两个复选框vibratePreference
和soundPreference
。我认为UserPreferencesFragment
不会实例化。以下是涉及的文件。我已经在这几个晚上了,我无法弄清楚我错过了什么。我在看什么?
谢谢,解放军
preferences_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>
preferences.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);
}
}
答案 0 :(得分:4)
我道歉。我的代码实际上一直在工作。我只是没有按下设置标题。当我把我的复选框出现时。这不是我想要的,所以我将PreferenceActivity
更改为下面的代码。
package com.hometelco.hfa.activities;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.util.Log;
import com.hometelco.hfa.R;
public class UserPreferencesActivity extends PreferenceActivity {
private static final String TAG = "UserPreferencesActivity";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate hasHeaders: " + hasHeaders());
getFragmentManager().beginTransaction().replace(android.R.id.content,
new UserPreferencesFragment()).commit();
}
/*
public void onBuildHeaders(List<Header> target) {
Log.i(TAG, "onBuildHeaders before load");
loadHeadersFromResource(R.xml.preference_headers, target);
Log.i(TAG, "onBuildHeaders after load");
}
*/
public static class UserPreferencesFragment extends PreferenceFragment {
private final static String TAG = "UserPreferencesFragment";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "fragment onCreate");
addPreferencesFromResource(R.xml.preferences);
}
}
}
感谢所有帮助我找到错误的人。
PLA
答案 1 :(得分:0)
我想我在这一行找到了错误:
android:fragment="com.hometelco.hfa.activities.UserPreferencesFragment"
查看docs,他们会显示使用packaganame.preferenceactivity$preferencefragment
格式调用该行...
我相信preferences_headers.xml中的行应该是:
android:fragment="com.hometelco.hfa.activities.UserPreferencesActivity$UserPreferencesFragment"
答案 2 :(得分:-1)
我认为你应该尝试:
addPreferencesFromResource(R.layout.preferences);
而不是:
addPreferencesFromResource(R.xml.preferences);
你还需要给你这样的复选框:
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);
if (checkBox.isChecked()) {
checkBox.setChecked(false);
}
您应该向您添加ID复选框。这是一个可能对您有帮助的链接:
http://developer.android.com/reference/android/widget/CheckBox.html