Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preferencefragment no view found for id android

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

like image 539
william pagnon Avatar asked Mar 11 '13 08:03

william pagnon


2 Answers

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.

like image 184
william pagnon Avatar answered Sep 30 '22 17:09

william pagnon


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.

like image 27
Ekta Bhawsar Avatar answered Sep 30 '22 15:09

Ekta Bhawsar