Strange Error using onCreateView in PreferenceFragment when calling addPreferencesFromResource from onCreate

后端 未结 3 439
慢半拍i
慢半拍i 2021-01-03 00:34

I\'m trying to add an ImageView to a preference fragment in order to show a preview of a color setting. I\'m accessing the instance of the imageview via the onCreateView met

相关标签:
3条回答
  • 2021-01-03 01:14

    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>
    
    0 讨论(0)
  • 2021-01-03 01:19

    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.

    0 讨论(0)
  • 2021-01-03 01:20

    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)

    0 讨论(0)
提交回复
热议问题