Search code examples
androidandroid-fragmentspreferenceactivity

PreferenceFragment / PreferenceActivity crashes - cant figure out why


I want to implement a preference screen. So I followed basically the example from the developer docs. When I start the Activity I see the header list, with one header (this part seems to work). But as soon I click this header the Activity crashed with below log.

I searched SO and google, but couldn't find any further information on this error.

I tried to shorten preference_appearance_screen.xml to one CheckBoxPreference item. Still same error on XML Line 18. So I guess that the error is not to be found in this file.

My activity class:

public class TrainerPreferenceActivity extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

    public static class AppearancePrefFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preference_appearance_screen);
        }
    }
}

preference_headers.xml file:

<?xml version="1.0" encoding="utf-8"?>
<preference-headers
    xmlns:android="http://schemas.android.com/apk/res/android">
    <header android:fragment=".TrainerPreferenceActivity$AppearancePrefFragment"
           android:title="Appearance"
           android:summary="An example of some preferences." />
</preference-headers>

preference_appearance_screen.xml file:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory 
        android:title="@string/pref_app_cat_theme">
    </PreferenceCategory>
    <PreferenceCategory 
        android:title="@string/pref_app_cat_background">
        <CheckBoxPreference 
            android:key="@string/pref_app_bg_iscustom_key" 
            android:title="@string/pref_app_bg_iscustom_title"
            android:summary="An example of some preferences."/>
        <CheckBoxPreference 
            android:key="@string/pref_app_bg_hascolor_key" 
            android:title="@string/pref_app_bg_hascolor_title"
            android:summary="An example of some preferences."/>  
        <CheckBoxPreference 
            android:key="@string/pref_app_bg_hasimage_key" 
            android:title="@string/pref_app_bg_hasimage_title"
            android:summary="An example of some preferences."/>
    </PreferenceCategory>
    <PreferenceCategory 
        android:title="@string/pref_app_cat_behavior">
        <CheckBoxPreference 
            android:key="@string/pref_app_orientationisenabled_key" 
            android:title="@string/pref_app_orientationisenabled_title"
            android:summary="An example of some preferences."/>        
        <CheckBoxPreference 
            android:key="@string/pref_app_gestureisenabled_key" 
            android:title="@string/pref_app_gestureisenabled_title"
            android:summary="An example of some preferences."/>
   </PreferenceCategory>
</PreferenceScreen>

LogCat:

04-30 17:22:01.215: E/AndroidRuntime(16775): FATAL EXCEPTION: main 04-30 17:22:01.215: E/AndroidRuntime(16775): java.lang.RuntimeException: Binary XML file line #18: You must supply a layout_width attribute. 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:491) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:5318) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.ViewGroup$LayoutParams.(ViewGroup.java:5271) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.AbsListView$LayoutParams.(AbsListView.java:5718) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.AbsListView.generateLayoutParams(AbsListView.java:5355) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.AbsListView.generateLayoutParams(AbsListView.java:86) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.LayoutInflater.inflate(LayoutInflater.java:477) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.preference.Preference.onCreateView(Preference.java:474) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.preference.Preference.getView(Preference.java:451) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:221) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.AbsListView.obtainView(AbsListView.java:2033) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.ListView.onMeasure(ListView.java:1127) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.View.measure(View.java:12723) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.View.measure(View.java:12723) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.View.measure(View.java:12723) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.View.measure(View.java:12723) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:999) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.LinearLayout.onMeasure(LinearLayout.java:555) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.View.measure(View.java:12723) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.View.measure(View.java:12723) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.View.measure(View.java:12723) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.View.measure(View.java:12723) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 04-30 17:22:01.215: E/AndroidRuntime(16775): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.View.measure(View.java:12723) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.os.Handler.dispatchMessage(Handler.java:99) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.os.Looper.loop(Looper.java:137) 04-30 17:22:01.215: E/AndroidRuntime(16775): at android.app.ActivityThread.main(ActivityThread.java:4424) 04-30 17:22:01.215: E/AndroidRuntime(16775): at java.lang.reflect.Method.invokeNative(Native Method) 04-30 17:22:01.215: E/AndroidRuntime(16775): at java.lang.reflect.Method.invoke(Method.java:511) 04-30 17:22:01.215: E/AndroidRuntime(16775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 04-30 17:22:01.215: E/AndroidRuntime(16775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 04-30 17:22:01.215: E/AndroidRuntime(16775): at dalvik.system.NativeStart.main(Native Method)


Solution

  • Alright, I set up a quick test project, but I haven't been able to reproduce the error - it displays and works fine on my Galaxy Nexus. What device are you running this on, and have you tried any others/emulators?

    The only possibly related issue I could find is this style/theming-related one, which seems to pop up every here and there. Have you made any customizations? If so, try running above code as 'vanilla' as possible to see if that makes a difference.