单击PreferenceActivity中的首选项标题后获得InflateException

时间:2015-08-19 06:28:24

标签: android preferences inflate-exception preference-headers

我正在为我的应用程序的首选项部分使用首选项标头。他们工作得很好,但不知何故发生了什么事(我无法实现),我开始得到InflateException。相关的代码和日志显示在下面,任何人都可以得到我的代码有什么问题吗?

PreferenceActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /*
     * Set back button
     */
    if (getActionBar() != null) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    /*
     * Set status bar color
     */
    ActivityUtil.setStatusBarColor(getWindow(), getResources());
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return (true);
    }
    return false;
}

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

@Override
protected boolean isValidFragment(String fragmentName) {
    return (ActivePassivePrefs.class.getName().equals(fragmentName)
            || SchedulePrefs.class.getName().equals(fragmentName)
            || GenderPrefs.class.getName().equals(fragmentName));
}

@Override
public void finish() {
    super.finish();
    overridePendingTransition(R.anim.slide_anim_righttoleft,
            R.anim.slide_anim_righttoleft_fromcenter);
}

ActivePassivePrefs:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        /*
         * Set back button
         */
    if (getActivity().getActionBar() != null) {
        getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    addPreferencesFromResource(R.xml.active_passive_app_prefs);
}

@Override
public void onResume() {
    super.onResume();
    registerListener();
}

@Override
public void onPause() {
    super.onPause();
    unregisterListener();
}

private void unregisterListener() {
    getPreferenceManager().getSharedPreferences()
            .unregisterOnSharedPreferenceChangeListener(this);
}
private void registerListener() {
    getPreferenceManager().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                      String key) {
    unregisterListener();
    Resources resources = getResources();
    if (key.equals(resources
            .getString(R.string.settings_enabledisable_checkbox_key))) {
        boolean isAppEnabled = sharedPreferences.getBoolean(key, Constants.DEFAULT_IS_APP_ENABLED);
        if (!isAppEnabled) {
            ActivityUtil.stopService(getActivity());
        } else {
            ActivityUtil.startService(sharedPreferences, getActivity(), resources);
        }
        TheBackupAgent.requestBackup(getActivity().getBaseContext());
    }
    registerListener();
}

GenderPrefs:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        /*
         * Set back button
         */
    if (getActivity().getActionBar() != null) {
        getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    addPreferencesFromResource(R.xml.gender_prefs);
    setSummary();
}

@Override
public void onResume() {
    super.onResume();
    registerListener();
}

@Override
public void onPause() {
    super.onPause();
    unregisterListener();
}

private void unregisterListener() {
    getPreferenceManager().getSharedPreferences()
            .unregisterOnSharedPreferenceChangeListener(this);
}
private void registerListener() {
    getPreferenceManager().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                      String key) {
    unregisterListener();
    Resources resources = getResources();
    if (key.equals(resources
            .getString(R.string.settings_gender_key))) {
        Gender[] genders = SuggestionData.getGenders(sharedPreferences, resources);
        SuggestionData.refreshSuggestionList(getResources().getAssets(), genders);
        setSummary();
        TheBackupAgent.requestBackup(getActivity().getBaseContext());
        SuggestionData.updateAppwidget(getActivity().getBaseContext());
    }
    registerListener();
}

private void setSummary() {
    ListPreference schedulePref = (ListPreference)findPreference(getResources().getString(R.string.settings_gender_key));
    SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
    String selected = prefs.getString(getResources().getString(R.string.settings_gender_key), getResources().getString(R.string.settings_gender_defaultval));
    int index = schedulePref.findIndexOfValue(selected);
    String entry = (String) schedulePref.getEntries()[index];
    schedulePref.setSummary(getResources().getString(R.string.settings_gender_summary) + "\n[" + entry + "]");
}

SchedulePrefs:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        /*
         * Set back button
         */
    if (getActivity().getActionBar() != null) {
        getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
    }
    addPreferencesFromResource(R.xml.schedule_prefs);
    setSummary();
}

@Override
public void onResume() {
    super.onResume();
    registerListener();
}

@Override
public void onPause() {
    super.onPause();
    unregisterListener();
}

private void unregisterListener() {
    getPreferenceManager().getSharedPreferences()
            .unregisterOnSharedPreferenceChangeListener(this);
}
private void registerListener() {
    getPreferenceManager().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                      String key) {
    unregisterListener();
    Resources resources = getResources();
    if (key.equals(resources
            .getString(R.string.settings_schedule_key))) {
        ActivityUtil.startService(sharedPreferences, getActivity(), resources);
        setSummary();
        TheBackupAgent.requestBackup(getActivity().getBaseContext());
    }
    registerListener();
}

private void setSummary() {
    Preference schedulePref = findPreference(getResources()
            .getString(R.string.settings_schedule_key));
    SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
    schedulePref.setSummary(getResources().getString(R.string.settings_schedule_summary)
            + "\n[" + prefs.getString(getResources()
                    .getString(R.string.settings_schedule_key),
            Constants.DEFAULT_SERVICE_TIME) + "]");
}

preference.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

preference_headers.xml:

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

<header
    android:fragment="com.btasdemir.stil.util.preference.ActivePassivePrefs"
    android:summary="@string/settings.enabledisable.summary"
    android:title="@string/settings.enabledisable.title">
</header>
<header
    android:fragment="com.btasdemir.stil.util.preference.SchedulePrefs"
    android:summary="@string/settings.schedule.summary"
    android:title="@string/settings.schedule.title" >
</header>
<header
    android:fragment="com.btasdemir.stil.util.preference.GenderPrefs"
    android:summary="@string/settings.gender.summary"
    android:title="@string/settings.gender.title" >
</header>

</preference-headers>

active_passive_app_prefs.xml:

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

<PreferenceCategory
   android:summary="@string/settings.enabledisable.summary"
   android:title="@string/settings.enabledisable.title">

    <CheckBoxPreference
            android:key="@string/settings.enabledisable.checkbox.key"
            android:title="@string/settings.enabledisable.checkbox.title"
            android:defaultValue="true" 
            android:summaryOn="@string/settings.enabledisable.checkbox.summaryon"
            android:summaryOff="@string/settings.enabledisable.checkbox.summaryoff" />

</PreferenceCategory>

</PreferenceScreen>

gender_prefs.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory
   android:summary="@string/settings.gender.summary"
   android:title="@string/settings.gender.title" >

   <ListPreference
       android:title="@string/settings.gender.title"
       android:key="@string/settings_gender_key"
       android:defaultValue="@string/genders_array_defaultvalue"
       android:entries="@array/genders_array"
       android:entryValues="@array/genders_array_values" />

</PreferenceCategory>

</PreferenceScreen>

schedule_prefs.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory
   android:summary="@string/settings.schedule.summary"
   android:title="@string/settings.schedule.title" >

    <com.btasdemir.stil.preference.TimePreference
            android:key="@string/settings.schedule.key"
            android:defaultValue="06:00"
            android:title="@string/settings.schedule.title"
            android:summary="@string/settings.schedule.summary" />

</PreferenceCategory>

</PreferenceScreen>

AndroidManifest中的StilPreferencesActivity定义:

    <activity
        android:name="com.btasdemir.stil.StilPreferencesActivity"
        android:label="@string/app_preferences_name"
        android:theme="@style/AppPreferenceTheme" >
    </activity>

此活动使用的样式:

<style name="AppTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <!-- Customize your theme here. -->
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:windowBackground">@drawable/background</item>
    <item name="android:actionBarItemBackground">@drawable/stil_item_selector</item>
    <item name="android:actionBarSize">@dimen/action_bar_height</item>
    <item name="android:actionOverflowButtonStyle">@style/AppTheme_ActionBar_Overflow</item>
    <item name="android:windowContentOverlay">@drawable/actionbar_shadow</item>
    <item name="android:homeAsUpIndicator">@drawable/ic_ab_up_compat</item>
</style>

<style name="AppPreferenceTheme" parent="AppTheme">
    <item name="android:listSeparatorTextViewStyle">@style/PreferenceListHeader</item>
    <item name="android:listViewStyle">@style/ListViewStyle</item>
    <item name="android:textColorSecondary">@color/DarkGray</item>
    <item name="android:textColor">@color/White</item>
    <item name="android:textColorPrimary">@color/White</item>
</style>

我在项目中使用的依赖项:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:support-annotations:22.2.1'
compile 'com.android.support:cardview-v7:22.2.1'
compile 'com.android.support:recyclerview-v7:22.2.1'
compile 'com.android.support:palette-v7:22.2.1'
compile 'com.google.android.gms:play-services:7.5.0'
compile project(':yWeatherGetter4a')
compile project(':ViewPagerIndicatorLibrary')
compile ('com.github.worker8:tourguide:1.0.12-SNAPSHOT@aar'){
    transitive=true
}
}

显示错误的日志部分:

08-18 15:38:59.715    5746-5746/com.btasdemir.stil E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.btasdemir.stil, PID: 5746
android.view.InflateException: Binary XML file line #18: Error inflating class <unknown>
        at android.view.LayoutInflater.createView(LayoutInflater.java:633)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.preference.Preference.onCreateView(Preference.java:510)
        at android.preference.Preference.getView(Preference.java:487)
        at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:246)
        at android.widget.AbsListView.obtainView(AbsListView.java:2349)
        at android.widget.ListView.onMeasure(ListView.java:1154)
        at android.view.View.measure(View.java:17554)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17554)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at android.view.View.measure(View.java:17554)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17554)
        at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1063)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
        at android.view.View.measure(View.java:17554)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:875)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17554)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at android.view.View.measure(View.java:17554)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:447)
        at android.view.View.measure(View.java:17554)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
        at android.view.View.measure(View.java:17554)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5891)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5273)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.

当我单击首选项屏幕中的一个首选项标题时会发生这种情况。

另外,当我深入调试代码时,我遇到了这个:

在类LayoutInflater和方法createView中,代码在创建首选项布局场景时抛出异常。确切的部分是TextView(我没有手动将其放在首选项xml中),TextView的根本原因属性是layout_height。

谢谢。

1 个答案:

答案 0 :(得分:0)

当我更改目标SDK版本时问题得到解决(我不记得我正在处理的版本我很抱歉,我写这篇文章是因为没有答案)。