Strange Error using onCreateView in PreferenceFragment when calling addPreferencesFromResource from onCreate
Asked Answered
P

3

6

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 method toset the test color, and it will display. However it only works if I don't call addPreferencesFromResource in the onCreate method - which is a problem since the preferences must be added. Also if I leave the call to addPreferencesFromResource, but remove the entire onCreateView method the program will run (albiet without the updatable imageview).

The error in both cases is "Content has view with id attribute 'android.R.id.list' that is not a ListView class"

I have tried to access the imageview from onCreate, but by then the layout items are inflated and I can't seem to access the actual instance that is displayed.

Error from LogCat:

04-11 00:42:43.619: E/AndroidRuntime(5362): FATAL EXCEPTION: main
04-11 00:42:43.619: E/AndroidRuntime(5362): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.signalwidget/com.example.android.signalwidget.SignalWidgetConfigure}: java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class

Here the PreferenceActivity with inline Fragment:

public class SigConfigure extends PreferenceActivity {

private static int prefs=R.xml.pref_widget_colors;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //getFragmentManager().beginTransaction().replace(android.R.id.content, new ColorsFragment()).commit();

}

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

public static class ColorsFragment extends PreferenceFragment {


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

        addPreferencesFromResource(SignalWidgetConfigure.prefs);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        super.onCreateView(inflater, container, savedInstanceState);

        //just testing to see if the imageview can be accessed.
        View v = inflater.inflate(R.layout.layout_pref_row, container, false);
        ImageView iv = (ImageView) v.findViewById(R.id.color_preview);
        iv.setBackgroundColor(Color.CYAN);

        return v;
    }


}}

Here is the preference definition in pref_widget_colors

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <Preference
        android:key="wifi_signal_color"
        android:title="WiFi Signal Color" >
        <intent
            android:action="android.intent.action.VIEW"
             android:targetClass="com.example.android.signalwidget.colorpicker.PickerActivity"
            android:targetPackage="com.example.android.signalwidget" />
    </Preference>
    <Preference
        android:key="cell_signal_color"
        android:title="Cell Signal Color" >
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.example.android.signalwidget.colorpicker.PickerActivity"
            android:targetPackage="com.example.android.signalwidget" />
    </Preference>

</PreferenceScreen>

Here is the layout containing the imageview in layout_pref_row.xml

<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="#aaa" />
</LinearLayout>

Despite the error I am not using a ListView or a ListFragment anywhere in my project. This almost seems like an android bug. Any suggestion would be appreciated.

Poseidon answered 11/4, 2013 at 5:14 Comment(0)
H
18

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>
Hero answered 7/5, 2013 at 18:7 Comment(2)
Oh that is awesome. Thank you Doctoror Drive! It would be quite helpful if the android page actual mentioned this while they're talking you through the code.Marden
@Alex could not find it, but the exception is pretty self-explanatory. Also, it's getting obvious if you look at the source code, since the PreferenceScreen inflates to a ListView, and the PreferenceFragment looks for it by android.R.id.content.Hero
M
1

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.

Marden answered 7/5, 2013 at 17:57 Comment(2)
Why the downvote? I had this exact problem and this was the problem that was causing it.Marden
I guess because this is sort of a workaround as it prevents the preferences from showing - not a solution?Tommie
C
1

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)

Chas answered 14/1, 2016 at 3:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.