PreferenceFragment: No view found for id android:id/prefs) for fragment

£可爱£侵袭症+ 提交于 2019-12-24 19:32:49

问题


I have a PreferenceActivity with a custom layout to keep a static header on top of all the items. I followed this thread to set this up:

Adding a header to a PreferenceActivity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/myCustomImageView"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:src="@mipmap/ic_launcher" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/myCustomImageView" />

</RelativeLayout>

and here is my Activity snippet:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // this makes a static header
    setContentView(R.layout.preferences_layout);

    // register as a listener
    PreferenceManager.getDefaultSharedPreferences(this)
            .registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onBuildHeaders(List<Header> target) {
    loadHeadersFromResource(R.xml.preference_headers, target);
}

yet, when I tap on an item it should spawn a fragment as my xml specifies:

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">

<header
    android:fragment="ui.UserPreferences"
    android:summary="@string/preferences_activity_user_settings_summary"
    android:title="@string/preferences_activity_user_settings_title" />

   more items....     

However, it does not. Instead, it crashes saying:

java.lang.IllegalArgumentException: No view found for id 0x1020463 (android:id/prefs) for fragment UserPreferences

I looked at a bunch of threads like the one below, but they did not help.

Android PreferenceFragment No view found for id for fragment

However if I just commented out the custom layout, the code works and the fragment shows up correctly. This works, in other words:

public class Preferences extends PreferenceActivity
        implements SharedPreferences.OnSharedPreferenceChangeListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // this works: By commenting out the custom header, the exception goes away
        //setContentView(R.layout.preferences_layout);

        // register as a listener
        PreferenceManager.getDefaultSharedPreferences(this)
                .registerOnSharedPreferenceChangeListener(this);
    }

I believe the fragment manager is not able to find the id because the listview is now nested inside a relativelayout. Yet, I have no idea how to fix this.

here's a bit of item fragment, however I don't think this is the problem:

public class UserPreferences extends PreferenceFragment implements
        Preference.OnPreferenceChangeListener,
        Preference.OnPreferenceClickListener,
        SharedPreferences.OnSharedPreferenceChangeListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /* Load the preferences from an XML resource */
        addPreferencesFromResource(R.xml.user_settings);

        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

    }

    more stuff...

Any help is greatly appreciated.

thank you.

来源:https://stackoverflow.com/questions/54894883/preferencefragment-no-view-found-for-id-androidid-prefs-for-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!