PreferenceFragment / PreferenceActivity崩溃 - 无法弄清楚原因

时间:2012-04-30 09:42:42

标签: android android-fragments preferenceactivity

我想实现偏好设置屏幕。所以我基本上遵循了developer docs的例子。 当我启动Activity时,我看到标题列表,带有一个标题(这部分似乎有效)。但是,一旦我单击此标题,活动就会在下面的日志中崩溃。

我搜索了SO和谷歌,但无法找到有关此错误的更多信息。

我尝试将 preference_appearance_screen.xml 缩短为一个CheckBoxPreference项。 XML Line 18上的错误仍然相同。所以我猜错误是在这个文件中找不到的。

我的活动类:

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文件:

<?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文件:

<?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):致命异常:主要   04-30 17:22:01.215:E / AndroidRuntime(16775):   java.lang.RuntimeException:二进制XML文件行#18:您必须提供   layout_width属性。 04-30 17:22:01.215:E / AndroidRuntime(16775):     在   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.measureHorizo​​ntal(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(原生方法)

2 个答案:

答案 0 :(得分:2)

好吧,我设置了一个快速测试项目,但我无法重现错误 - 它在我的Galaxy Nexus上显示并正常工作。您正在使用什么设备运行此功能,并且您是否尝试过其他设备/模拟器?

我能找到的唯一可能相关的问题是this style/theming-related one,它似乎随处可见。你做过任何定制吗?如果是这样,尝试在代码上运行尽可能“香草”,看看是否有所作为。

答案 1 :(得分:2)

这是因为您引用了首选项屏幕正在使用的应用程序的自定义样式,并且该自定义样式未继承这些必需属性。要保留自定义样式并避免此问题,请确保自定义样式具有“layout_width”和“layout_height”属性。在你的情况下:

    <style name="MyListSeperator" parent="android:Widget.Holo.Light.TextView">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">wrap_content</item>
        ...
    </style>

然后你继续取消评论:

    <item name="android:listSeparatorTextViewStyle">@style/MyListSeparator</item>