使用PreferenceActivity对xml文件进行膨胀时出错

时间:2014-11-18 23:30:56

标签: java android xml preferenceactivity

我的pref_general.xml文件存在一些问题,因为我收到以下错误消息:

  

android.view.InflateException:二进制XML文件行#14

然后logcat将该行指向我的PreferenceActivity,我称之为“addPreferencesFromResource(R.xml.pref_general);”

经过几个小时的尝试解决后,我仍然不知道问题是什么,所以也许有人对此很熟悉,或者只是另一组眼睛就能找到问题。

此外,我在具有viewPager的App中使用它,并且从MainActivity的onOptionsItemSelected()方法显式启动SettingsActivity。 我不认为这很重要(因为其他活动正确启动)但永远不会知道...

THX !!

第14行在我的ListPreference组件上。

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>

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;
  }
}

1 个答案:

答案 0 :(得分:0)

好的,我找到了:

事实上,最新版本的Android Studio现在为每种类型创建了两个值文件。 所以我的strings.xml用于w820dp ...因此排除了我正在测试的手机。

愚蠢的事情,但这就是我们学习的方式......

希望这篇文章对其他人有用!!!