preferencefragment找不到ID为android的视图 [英] preferencefragment no view found for id android

查看:79
本文介绍了preferencefragment找不到ID为android的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不赞成使用getpreferencescreen,所以我尝试实现标头首选项活动.

I had the getpreferencescreen deprecated so I tryed to implement the header preference activity.

这是我的代码:

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)的视图

I've got the error:No view found for id 0x1020343 (android:id/prefs) for fragment

XML代码pref_headers.xml:

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>

->

XML代码preferences1.xml:

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>

预先感谢

推荐答案

官方文档具有误导性:

删除public class EditPreferences extends PreferenceActivity中的onCreate功能可以解决此问题.

Removing the onCreate function in public class EditPreferences extends PreferenceActivity resolves the issue.

我只是将onCreate中的内容移到了onBuildHeaders函数中.

I just moved what was in onCreate to onBuildHeaders function.

这篇关于preferencefragment找不到ID为android的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆