Strange error when using onCreateView in PreferenceFragment when calling addPreferencesFromResource from onCreate

I am trying to add an ImageView to a preference fragment to show a preview of a color setting. I access the instance of the image using the onCreateView method to adjust the color of the test and it will be displayed. However, it only works if I do not call addPreferencesFromResource in the onCreate method, which is a problem since preferences must be added. Also, if I leave the addPreferencesFromResource call but delete the entire onCreateView method, the program will start (albiet without an updated image).

Error in both cases: "Content has a view with id attribute" android.R.id.list, which is not a ListView class "

I tried to access the image view from onCreate, but by then the layout elements were overpriced and I could not access the actual instance that was being displayed.

LogCat Error:

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's 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 image 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 do not use ListView or ListFragment anywhere in my project. It almost looks like an Android bug. Any suggestion would be appreciated.

+6
source share
3 answers

When you create a custom parameter for PreferenceActivity or PreferenceFragment, you must supply a ListView with the identifier android.R.id.list where preferences go.

 <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> 
+18
source

I had this problem today. This is due to the use of Android Tutorials here: http://developer.android.com/guide/topics/ui/settings.html#Fragment

Basically, I found that the problem is related to using the preferences.xml file:

 addPreferencesFromResource(R.xml.preferences); 

As soon as I commented on this, the error disappeared, and my activity began to show, although it was not loaded with preferences. I will try to track this down and trace it here.

I hope this helps so far! The exception thrown is not very useful.

+1
source

I had the same problem and the solution was to import the correct fragment library:

 import android.app.Fragment; 

(and not the version from the support library)

+1
source

All Articles