9

我不推荐使用 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 代码 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>

-->

XML 代码首选项1.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>

提前致谢

4

2 回答 2

7

官方文档具有误导性:

删除该onCreate功能public class EditPreferences extends PreferenceActivity可解决此问题。

我只是移动了其中的onCreate功能onBuildHeaders

于 2013-03-19T08:26:25.207 回答
1

我也遇到了这个问题,直到我意识到我setContentView()onCreate().FragmentActivity

FragmentTransaction.add()在您的情况下,传入的 idR.id.feedContentContainer必须是 setContentView() 中指定的布局的子级。

您没有向我们展示您的onCreate()方法,所以也许这是同一个问题。

于 2018-04-07T06:09:53.913 回答