我不推荐使用getpreferencescreen,因此我尝试实现标头首选项活动。
这是我的代码:
public class EditPreferences extends PreferenceActivity {
ListPreference m_list_preference_dive_centre_rating;
ListPreference m_list_preference_dive_region_rating;
ListPreference m_list_preference_wreck;
ListPreference m_list_preference_cave;
MultyChoiceListPlugin m_list_preference_marine_life;
ListPreference m_list_preference_region;
Preference m_order_by_name;
SharedPreferences.Editor m_editor;
static Context m_context;
int m_order_type_val = 0;
boolean m_wreck_selected = false;
boolean m_cave_selected = false;
boolean m_marine_life_selected = false;
boolean m_region_selected = false;
int number_click_wreck = 0;
int number_click_cave = 0;
int number_click_marine_life = 0;
int number_click_region = 0;
boolean m_without_guide_selected = false;
boolean m_without_equipment_selected = false;
boolean m_budget_selected = false;
boolean m_region_rating = false;
boolean m_centre_rating = false;
private ListView m_preferenceView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//m_order_by_name.setOnPreferenceClickListener(onOrderByNamePrefClick);
final Button back_btn = (Button)this.findViewById(R.id.back);
final Button home_btn = (Button)this.findViewById(R.id.home);
//final Button search_btn = (Button)this.findViewById(R.id.search);
final Button info_btn = (Button)this.findViewById(R.id.info);
back_btn.setOnClickListener(onBackBtn);
home_btn.setOnClickListener(onHomeBtn);
// search_btn.setOnClickListener(onSearchBtn);
info_btn.setOnClickListener(onInfoBtn);
}
/**
* Populate the activity with the top-level headers.
*/
@Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.pref_headers, target);
}
public static class Prefs1Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.wrap_preferences);
// Make sure default values are applied. In a real app, you would
// want this in a shared function that is used to retrieve the
// SharedPreferences wherever they are needed.
//PreferenceManager.setDefaultValues(getActivity(),
// R.layout.wrap_preferences, false);
addPreferencesFromResource(R.xml.preferences1);
}
}
public static class Prefs2Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Make sure default values are applied. In a real app, you would
// want this in a shared function that is used to retrieve the
// SharedPreferences wherever they are needed.
//PreferenceManager.setDefaultValues(getActivity(),
// R.xml.preferences, false);
addPreferencesFromResource(R.xml.preferences2);
}
}
public static class Prefs3Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Make sure default values are applied. In a real app, you would
// want this in a shared function that is used to retrieve the
// SharedPreferences wherever they are needed.
// PreferenceManager.setDefaultValues(getActivity(),
// R.xml.preferences, false);
addPreferencesFromResource(R.xml.preferences3);
}
}
private View.OnClickListener onBackBtn = new OnClickListener() {
public void onClick(View v) {
//set the view for dive center list ordered by rating
Intent intent_call_back = new Intent();
intent_call_back.putExtra("goback","last_from");
setResult(DivingReview.CALL_BACK,intent_call_back);
finish();
}
};
private View.OnClickListener onHomeBtn = new OnClickListener() {
public void onClick(View v) {
//set the view for dive center list ordered by rating
//set the result to the callback of the parent activitie
Intent intent_call_back = new Intent();
intent_call_back.putExtra("goback","home");
setResult(DivingReview.CALL_BACK,intent_call_back);
finish();
}
};
我收到错误:找不到ID为0x1020343(android:id / prefs)的片段
XML code pref_headers.xml:
<?xml version="1.0" encoding="UTF-8"?>
<preference-headers
xmlns:android="http://schemas.android.com/apk/res/android" >
<header android:fragment="com.freelancerobotics.DivingReview.EditPreferences$Prefs1Fragment"
style="@style/pref_style"
android:key="who_is_the_best"
android:title="@string/who_is_the_best_text"
android:summary="@string/who_is_the_best_summary">
</header>
<header android:fragment="com.freelancerobotics.DivingReview.EditPreferences$Prefs2Fragment"
android:title="@string/what_is_your_budget_text"
android:summary="@string/what_is_your_budget_summary" >
</header>
<header android:fragment="com.freelancerobotics.DivingReview.EditPreferences$Prefs3Fragment"
android:title="@string/order_by_name_title"
android:summary="@string/order_by_name_summary">
</header>
<!-- <header
android:icon="@drawable/icon_question"
android:title="Blackmoon Info Tech Services"
android:summary="link to BITS blog">
<intent android:action="android.intent.action.VIEW"
android:data="http://www.blackmoonit.com/2012/07/all_api_prefsactivity/" />
</header>
- &GT;
XML code preferences1.xml:
<?xml version="1.0" encoding="UTF-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/pref_style"
android:key="who_is_the_best"
android:title="@string/who_is_the_best_text"
android:summary="@string/who_is_the_best_summary">
<PreferenceCategory
android:title="@string/who_is_the_best_dive_centre_text"
android:key="who_is_the_best_dive_centre_category">
<ListPreference
android:key="select_dive_centre_rating"
android:title="@string/select_dive_centre_rating_text"
android:summary="@string/select_dive_centre_rating_summary"
android:entries="@array/search_dive_centre_names"
android:entryValues="@array/search_dive_centre_clauses"
android:dialogTitle="@string/search_dive_centre_choice_title"/>
</PreferenceCategory>
</PreferenceScreen>
提前致谢
答案 0 :(得分:7)
官方文件具有误导性:
删除onCreate
中的public class EditPreferences extends PreferenceActivity
功能可解决此问题。
我刚刚将onCreate
中的内容移动到onBuildHeaders
函数。
答案 1 :(得分:1)
我也遇到了这个问题,直到我意识到我在setContentView()
的{{1}}方法的onCreate()
中指定了错误的布局。
传入FragmentActivity
的id(在您的情况下为FragmentTransaction.add()
)必须是setContentView()中指定的布局的子级。
您没有向我们展示您的R.id.feedContentContainer
方法,因此这可能是同样的问题。