Strange Error using onCreateView in PreferenceFragment when calling addPreferencesFromResource from onCreate

五迷三道 提交于 2019-11-30 14:32:34

When you create a custom layot for a PreferenceActivity or a PreferenceFragment you must supply a ListView with id android.R.id.list where the Preferences go to.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/color_preview"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="5dp"
        android:background="#aaaaaa" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dp" />

</LinearLayout>

I had this problem today as well. It stemmed from using the Android Tutorials here: http://developer.android.com/guide/topics/ui/settings.html#Fragment

Basically, I found the problem came from using the preferences.xml file:

addPreferencesFromResource(R.xml.preferences);

Once I commented this out, the error went away and my activity started showing, although unpopulated by preferences. I'll try to hunt this down and follow up here.

I hope this helped so far! The exception thrown is not very helpful.

I had the same problem, and the solution was to ensure that I was importing the correct Fragment library:

import android.app.Fragment;

(not the version from the support library)

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