preferencefragment no view found for id android
Asked Answered
M

2

9

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

here is my code:

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();
    }
};

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

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 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>

thanks in advance

Maxantia answered 11/3, 2013 at 8:14 Comment(0)
M
7

The official documentation is misleading:

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

I just moved what was in onCreate to onBuildHeaders function.

Maxantia answered 19/3, 2013 at 8:26 Comment(1)
I tried this but it does not work if you have nested preferences as fragments?Sherrillsherrington
B
1

I was having this problem too, until I realized that I had specified the wrong layout in setContentView() of the onCreate() method of the FragmentActivity.

The id passed into FragmentTransaction.add(), in your case R.id.feedContentContainer, must be a child of the layout specified in setContentView().

You didn't show us your onCreate() method, so perhaps this is the same problem.

Borisborja answered 7/4, 2018 at 6:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.