Search code examples
javaandroidxmlpreferenceactivity

Error inflating xml file with PreferenceActivity


I'm having some issue with my pref_general.xml file as I get the following error message:

android.view.InflateException: Binary XML file line #14

Then the logcat points the line in my PreferenceActivity where I call "addPreferencesFromResource(R.xml.pref_general);"

After several hours of trying to sort it out I still have no idea what the issue is so maybe someone is familiar with this or simply another set of eyes can find the problem.

Also, I use it in an App which has a viewPager and the SettingsActivity is launched explicitly from the MainActivity's onOptionsItemSelected() method. I dont think it's important (as other Activities are launched properly) but never know...

Thx!!

Line 14 is on my ListPreference component.

pref_general.xml:

?xml version="1.0" encoding="utf-8"?>

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

<EditTextPreference
  android:title="@string/pref_location_label"
  android:key="@string/pref_location_key"
  android:defaultValue="@string/pref_location_default"
  android:inputType="text"
  android:singleLine="true"/>

<ListPreference
  android:title="@string/pref_units_label"
  android:key="@string/pref_units_key"
  android:defaultValue="@string/pref_units_metric"
  android:entryValues="@array/pref_units_values"
  android:entries="@array/pref_units_options"/>

</PreferenceScreen>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <string name="title_activity_settings">Settings</string>

  <!-- Strings related to Settings -->

  <!-- Label for the location preference [CHAR LIMIT=30] -->
  <string name="pref_location_label">Location</string>

  <!-- Key name for storing location in SharedPreferences [CHAR LIMIT=NONE] -->
  <string name="pref_location_key" translatable="false">location</string>

  <!-- Default postal code for location preference [CHAR LIMIT=NONE] -->
  <string name="pref_location_default" translatable="false">-36.8799074,174.7565664</string>

  <!-- Label for the temperature units preference [CHAR LIMIT=30] -->
  <string name="pref_units_label">Temperature Units</string>

  <!-- Label for metric option in temperature unit preference [CHAR LIMIT=25] -->
  <string name="pref_units_label_metric">Metric</string>

  <!-- Label for imperial option in temperature unit preference [CHAR LIMIT=25] -->
  <string name="pref_units_label_imperial">Imperial</string>

  <!-- Key name for temperature unit preference in SharedPreferences [CHAR LIMIT=NONE] -->
  <string name="pref_units_key" translatable="false">units</string>

  <!-- Value in SharedPreferences for metric temperature unit option [CHAR LIMIT=NONE] -->
  <string name="pref_units_metric" translatable="false">metric</string>

  <!-- Value in SharedPreferences for imperial temperature unit option [CHAR LIMIT=NONE] -->
  <string name="pref_units_imperial" translatable="false">imperial</string>

</resources>

arrays.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <string-array name="pref_units_options">
    <item>@string/pref_units_label_metric</item>
    <item>@string/pref_units_label_imperial</item>
  </string-array>

  <string-array name="pref_units_values">
    <item>@string/pref_units_metric</item>
    <item>@string/pref_units_imperial</item>
  </string-array>


</resources>

And the PreferenceActivity:

public class SettingsActivity extends PreferenceActivity implements Preference.OnPreferenceChangeListener{

private static final boolean ALWAYS_SIMPLE_PREFS = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Add 'general' preferences, defined in the XML file
    addPreferencesFromResource(R.xml.pref_general);

    // For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
    // updated when the preference changes.
    bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
    bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_units_key)));

}


private void bindPreferenceSummaryToValue(Preference preference) {
    // Set the listener to watch for value changes
    preference.setOnPreferenceChangeListener(this);

    // Trigger the listener immediately with the preference's current value
    onPreferenceChange(preference, PreferenceManager
            .getDefaultSharedPreferences(preference.getContext())
            .getString(preference.getKey(), ""));
}

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
    String stringValue = newValue.toString();

    if (preference instanceof ListPreference) {
        // For list preferences, look up the correct display value in
        // the preference's entries list (since they have a separate labels/values)
        ListPreference listPreference = (ListPreference) preference;
        int prefIndex = listPreference.findIndexOfValue(stringValue);
        if (prefIndex >= 0) {
            preference.setSummary(listPreference.getEntries()[prefIndex]);
        }
    } else {
        // For other preferences, set the summary to the value's simple string representation
        preference.setSummary(stringValue);
    }
    return true;
  }
}

Solution

  • Ok, I found it:

    In fact, the latest version of Android Studio now creates two values files for each type. So my strings.xml were for w820dp...therefor excluding the phone I'm testing on.

    Silly things but that's how we learn...

    Hopefully this post will be useful to someone else!!!