Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange Error using onCreateView in PreferenceFragment when calling addPreferencesFromResource from onCreate

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.

like image 812
Enoch Root Avatar asked Apr 11 '13 05:04

Enoch Root


2 Answers

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>
like image 85
Yaroslav Mytkalyk Avatar answered Nov 23 '22 17:11

Yaroslav Mytkalyk


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.

like image 27
John Kroetch Avatar answered Nov 23 '22 18:11

John Kroetch